JavaScript | 语法(重点ForIn遍历)

一、if语句

<html>
<head>
    <meta charset="UTF-8" />
    <title></title>
</head>
<body>
</body>
<script>
    score=90;
    if(score>=90){
        alert('A');
    }else if(score>=60){
        alert('B');
    }else{
        alert('C');
    }
</script>
</html>

if-elseif-else特点:

1.条件必须成立

2.只能命中其一

二、switch语句

<html>
<head>
    <meta charset="UTF-8" />
    <title></title>
</head>
<body>
</body>
<script>
    week=45;
    switch(week){
        case 1: alert('Monday');  break;
        case 2: alert('Tuesday'); break;
        case 3: alert('Wednesday'); break;
        case 4: alert('Thursday'); break;
        case 5: alert('Friday'); break;
        case 6: alert('Saturday'); break;
        case 7: alert('Sunday'); break;
        default: alert('NOT FOUND'); break;
    }
</script>
</html>

三、while循环

<html>
<head>
    <meta charset="UTF-8" />
    <title></title>
</head>
<body>
</body>
<script>
    i=0;
    while(i<10){
        document.write('<h1>'+i+'<h1>');
        i++;
    }
</script>
</html>

四、for循环

<html>
<head>
    <meta charset="UTF-8" />
    <title></title>
</head>
<body>
</body>
<script>
    for(i=0;i<3;i++){
        document.write('<h1>'+i+'<h1>');
    }
</script>
</html>

五、forIn遍历

<html>
<head>
    <meta charset="UTF-8" />
    <title></title>
</head>
<body>
</body>
<script>
    obj={'username':'user','age':'20','sex':'female'};

    for(i in obj){
        document.write('<h1>'+i+': '+obj[i]+'</h1>');
    }
</script>        
</html>

实例:九九乘法表

<html>
<head>
    <meta charset="UTF-8" />
    <title></title>
</head>
<body>
</body>
<script>
    for(i=1;i<=9;i++){
        document.write('<h3>');
        for(j=1;j<=i;j++){
            a=i*j;           
            document.write('<span>'+j+'x'+i+'='+a+'</span> ');
        }
        document.write('</h3>');
    }
</script>        
</html>

拓展:n*n乘法表

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值