if 语句:
语法一:
if(condeition){
statement1;
}
<script type="text/javascript">
var age = 15;
if(age<18){
alert("你还没有成年");
console.log("你还没有成年你跑这来干啥啊!");
}else{
console.log("Lod司机带带我!");
}
</script>
<script type="text/javascript">
var age = 15;
if(age<18){
alert("你还没有成年");
console.log("你还没有成年你跑这来干啥啊!");
}else if(age>=18 && age<=59){
alert("通过许可");
console.log("Lod司机带带我!");
}else{
console.log("Lod司机带带我!");
}
</script>
prompt
<script type="text/javascript">
var age=prompt("亲输入你的年龄");
if(age<18){
alert("你还没有成年");
console.log("你还没有成年你跑这来干啥啊!");
}else if(age>=18 && age<=59){
alert("通过许可");
console.log("Lod司机带带我!");
}else{
console.log("Lod司机带带我!");
}
</script>
image.png
demo练习:
demo练习01
补充获取字符串长度:
补充获取字符串长度
<script type="text/javascript">
var passwd = prompt("亲输入6位密码");
if(passwd.length == 6){
if(isNaN(passwd) != true){
alert("设置成功");
}else{
alert("必去全部是数学谢谢!");
}
}else{
alert("你输入的长度不正确!");
}
</script>
补充获取星期
补充获取星期
<script type="text/javascript">
var week = new Date().getDay()
switch(week){
case 0:
document.write("Taday is 星期日");
break;
case 1:
document.write("Taday is 星期一");
break;
case 2:
document.write("Taday is 星期二");
break;
case 3:
document.write("Taday is 星期三");
break;
case 4:
document.write("Taday is 星期四");
break;
case 5:
document.write("Taday is 星期五");
break;
case 6:
document.write("Taday is 星期日六");
break;
default:
alert("CUOWU");
break;
}
console.log(week);
</script>
for循环语句
<script type="text/javascript">
for(var a=1;a<=100;a++){
document.write(a,"<br/>");
}
for(var a=100;a>=0;a--){
console.log(a);
}
</script>
循环嵌套
小测试(For循环完成99乘法表)
For循环完成99乘法表
<script type="text/javascript">
for(var a=1;a<=9;a++){
for(var b=1;b<=a;b++){
document.write(b+"*"+a+"="+a*b+" ");
}
document.write("<br/>");
}
</script>
while循环
<script type="text/javascript">
var a=1;
while(a<=100){
console.log(a+"\n");
a++;
}
</script>
for循环与while
image.png函数的作用
argumentsarguments
JS在非严格模式可以通过arguments修改参数
<script type="text/javascript">
function CNM(){
var b,sum=0;
var c = arguments.length
for(var a=0;a<c;a++){
b = arguments[a];
sum = sum + b;
}
return sum;
}
var c = CNM(45,405,48,4848,56,564,48,1512,1321,48);
console.log(c);
</script>
网友评论