JS 零基础入门——04(数组)

数据类型:数组类型
数组:用来存储一系列相同类型的元素的容器


   在js中存储5个学生的成绩

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
<script>
   var score=70
   var score1=55                         方法1
   var score2=60
   var score3=30
   var scare4=90
   console.log(score)
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
<script>
   var score=new Array(5)
   scores[0]=50
   scores[1]=90
   scores[2]=100                         方法二;原型创建法                     
   scores[3]=44                
   scores[4]=60
   console.log(scores[2])
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
<script>
   var scores=[60,55,80,100,35]            方法三:直接定义法
   console.log(scores[2])
</script>
</body>
</html>


   数组的遍历:依次的拿出数组中元素


案例一:将数组中的元素 依次的输出到命令行

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
<script>
   var scores=[60,55,80,100,35]
    console.log(scores[0])
    console.log(scores[1])                   方法一
    console.log(scores[2])    
    console.log(scores[3])
    console.log(scores[4])
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
    <title></title>
</head> 
<body>                                       方法二
<script>
    var scores=[60,55,80,100,35]
    for(var n=0;n<scores.length;n++){
        console.log(scores[n])
    }
</script>
</body>
</html>

案例二:

求总分 :依次的将数组中的元素求和 

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
<script>
    var scores=[60,55,80,100,35]
    var sum=0
    for(var n=0;n<scores.length;n++){
        sum=sum+scores[n]
    }
    console.log(sum)
</script>
</body>
</html>

案例三:

求最大值:求最值的思想   将最大值输出到控制台

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
<script>
     var scores=[60,55,80,100,35]                 假定思想
     var max=scores[0]
     for(var n=1;n<scores.length;n++){
        if(scores[n]>max){
            max=scores[n]
        }
     }
     console.log(max)
</script>
</body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值