<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> </head> <body> </body> </html> <script type="text/javascript"> //循环语句:满足判断条件继续执行循环,不满足判断条件停止执行。 //1.while循环:先判断条件,再执行循环 /*var one = 0; var two = 0; while(one <= 20){ two = two + one; one++; } alert(two);*/ //2.do-while循环:循环体内的代码,至少执行一次 /*var one = 0; var two = 0; do{ two = two + one; one++; }while (two < 30); alert(one);*/ //for循环 /*var two = 0; for(var one = 0; one <= 20; one++){ two = two + one; } alert(two);*/ //跳转语句 //1.continue(跳过) for(var i = 1; i <= 10;i++){ if(i == 6){ continue; } alert(i); } //2.break(结束) for(var i = 1;i <= 10;i++){ if(i == 6){ break;} alert(i); } //数组
//定义方式
//空数组的定义方式
var attr = new Array();
var attr = [];
var attr =new Array(3);
//有值的数组定义
var attr = new Array("zhangsan","lisi","wangwu");
var attr2 = ["zhangsan","lisi","wangwu"];
alert(attr2.length);
//数组的遍历
//1.for遍历
for(var i = 0;i < attr2.length;i++){
alert(attr2[i]);
}
//2. console.log(attr2);
var attr2 = ["zhangsan","lisi","wangwu"];
0: "zhangsan"
1:"sili"
2:'wangwu'
length:3
//3. for-in 遍历
for(var k in attr2){
alert(attr2[k]);
}
//数组的赋值:
//定义一个空数组,对它进行赋值
var aa = new Array();
aa[0] = "茄子";
aa[1] = "西红柿";
aa[2] = "菜椒";
aa[3] = "黄瓜";
console.log(aa);
//循环赋值
for(var i = 0; i <= 5;i++){
attr[i] = i + 1;
}
attr = [1,2,3,4,5,6];
console.log(attr);
/*小练习题1:1-n求和练习题 var two = 0; for(var one = parseInt(prompt("请输入一个数值"));one >= 1;one --){ two = two + one; } alert(two);*/ //2.输入台阶数,求总得分: /*var jieShu = parseInt(prompt("请输入您所在的阶数")); var fen = 0; if(jieShu <= 49){ for(var n = jieShu;n >=1;n-- ){ fen = fen + n; } }else if(jieShu > 49 && jieShu <= 100){ fen = (jieShu - 49) * 10 + 1225; }else{ fen = "输入错误,请输入一到一百的整数" } alert(fen);*/ //3:一张纸厚0.0001米,喜马拉雅山高8848米,叠多少次可以达到? /*var ci = 0; for(var zhi = 0.0001;zhi < 8848; zhi *= 2){ ci++; } alert(ci + "次");*/ //一张纸厚0.0001米,喜马拉雅山高8848米,叠多少次可以达到? /*var gao = 0.0001; var i = 0; while(gao < 8848){ i++; gao = gao * 2 } alert(i);*/ //4.篮球练习题:篮球从100米高度落下,每次重新弹起高度是原先的0.6,落到一米需要多少次? /*var ci = 0; for(var zhi = 100;zhi > 1; zhi *= 0.6){ ci++; } alert(ci + "次");*/ //5.列出100内的所有偶数 /*for(var one = 0; one <= 100 ;one++){ if(one % 2 == 0){ two = one; document.write(two + "<br />"); } }*/ /*6.九九乘法表 for(var x = 1;x <= 9;x++){ for(var y = 1;y <= x;y++){ document.write( x + "*" + y + "=" + x * y+" "); } document.write("<br />"); }*/ /*7.100以内与7相关的数 var str =""; for(var yi = 1; yi <= 100;yi ++){ if(yi % 7 == 0){ str += yi+","; }else if((yi - 7) % 10 == 0 ){ str += yi+","; } else if(yi > 70 && yi < 80){ str += yi+","; } } document.write(str);*/ //8.求100以内的质数 /*for(var n = 2;n <= 100;n++){ var m = 1; for(var x = 2; x < n;x++){ if(n % x == 0){ m = 0; } } if(m == 1){ console.log(n); } }*/ //9.购物卡问题:有一百元购物卡,牙刷单价5元,香皂单价2元;洗发水单价15元,有多少种方法可以把一百元刚好用完? /*var n = 0; for(var y = 0;y <= 20;y++){ for(var x = 0;x <=50;x++){ for(var s = 0; s <= 6;s++){ if((5 * y + 2 * x + 15 * s) == 100){ document.write("牙刷:" + y + "支," + "香皂:" + x + "块," + "洗发水" + s + "瓶," + "<br />") n++; } } } } console.log(n);*/ </script>
练习题1:
练习题2:
练习题3:
练习题4:
练习题5:
练习题6:
练习题7:
练习题8:
练习题9: