JavaScript条件语句

学习目标

1.掌握length属性的应用

2.掌握if语句的嵌套

length

语法:string.length

功能:获取string字符串的长度

返回值:number

 

<script>
       var password=prompt("请设置您的密码");
       // 判断密码的长度,如果不是6位,否则
       if(password.length!=6){
          alert("请输入6位的数字密码");
       }else{
             // 如果密码是非数字,否则是数字
          if(isNaN(password)){
             alert("密码必须要是数字");
          }else{
             alert("密码设置正确");
          }
       }
    </script>

 

<script>
      var str="abc123";
      var num=parseInt(str);
      if(num==NaN){   // NaN和任何内容都不相等,包括它本身
         alert(NaN);
      }else if(num==123){
           alert(123);
      }else if(typeof num=="number"){
         alert("num");
      }else{
           alert("str");
      }
      // num
    </script>

 

 

JavaScript条件语句

学习目标

1.掌握星期的获取方法

2.掌握document.write()

3.掌握条件语句switch

获取星期

语法:new Date().getDay()

功能:获取星期

返回值:number(0-6)

输出

语法:document.write(“内容”)

功能:向浏览器输出内容

switch语句

语法:  switch(expression){

           case value:statement

           break;

           case value:statement

           break;

           ......

           default:statement

}

 

<script>
       var week=new Date().getDay();
       var weekstr="";
       console.log(week);  // 0-6
       // 多条件的判断 switch
       switch(week){
          case 0:
          weekstr="日";
          break;  // 退出
          case 1:
          weekstr="一";
          break;
          case 2:
          weekstr="二";
          break;
          case 3:
          weekstr="三";
          break;
          case 4:
          weekstr="四";
          break;
          case 5:
          weekstr="五";
          break;
          default:
          weekstr="六";
       }
       document.write("今天是星期"+weekstr);
    </script>

 

<script>

 

 var score=prompt("请输入成绩");
       var result="";
       if(isNaN(score)){
          alert("成绩必须是数字");
       }else{
          if(score<0 || score>100){
             alert("您输入的成绩有误");
          }else{
             switch(parseInt(score/10)){
                case 0:
                case 1:
                case 2:
                case 3:
                case 4:
                case 5:
                result="不及格";
                break;
                case 6:
                result="及格";
                break;
                case 7:
                result="一般";
                break;
                case 8:
                result="良";
                break;
                case 9:
                result="优";
                break;
                default:
                result="满分"
             }
             alert("您的成绩为:"+result);
          }
       }

 

</script>

 

<script>
       var score=prompt("请输入您的成绩");
       var level="";
       // 成绩的类型:如果成绩是非数字
       if(isNaN(score)){
          alert("成绩必须是数字");
       }else{
          if(score<0 || score>100){
             alert("成绩输入有误!");
          }else{
             switch(parseInt(score/10)){
                 case 0:
                 case 1:
                 case 2:
                 case 3:
                 case 4:
                 case 5:
                 level="不及格";
                 break;
                 case 6:
                 level="及格";
                 break;
                 case 7:
                 level="一般";
                 break;
                 case 8:
                 level="良";
                 break;
                 case 9:
                 level="优";
                 break;
                 default:
                 level="满分";
             }
             alert("您的成绩是"+level);
          }
       }
    </script>

 

 

 

 

posted on 2017-10-06 10:36  欧阳贝贝  阅读(1943)  评论(0编辑  收藏  举报