for循环在js里,貌似很重要,而且还挺让人晕头转向的…这个需要多练习了
<script type="text/javascript">
// 1-100所有整数的和与平均值
var he = 0;
for (var i=0;i<=100;i++) {
he = he + i;
}
console.log('1-100的整数和是:' + he);
console.log('1-100的平均值为:' + he / 100);
// 1-100所有偶数的和
var he2 = 0;
for (var shu = 1;shu <= 100;shu++) {
if(shu % 2 != 0){
continue;
} else {
he2 = he2 + shu;
}
}
console.log('偶数的和为:' + he2);
//使用while 1-100实现偶数的和
var he4 = 0;
var shu3 = 1;
while ( shu3 <= 100) {
shu3++;
if (shu3 % 2 != 0) {
continue;
}
he4 = he4 + shu3;
}
console.log('while实现的1-100偶数的和是:' + he4)
// 100以内7的倍数的和
var he3 = 0;
for (var shu2 = 1;shu2 <= 100;shu2++) {
if (shu2 % 7 ==0) {
he3 = he3 + shu2;
}
}
console.log('100以内7的倍数的和:' + he3);
// 用☆打印一个矩形,要求5*5
var str = '';
for (var x = 1;x <=5 ; x++) {
for (var y = 1;y <=5 ;y++) {
str = str + '☆';
}
str = str + '\n';
}
console.log(str);
// 使用☆用for循环打印一个三角形
var str2 = '';
for (var z = 1;z <= 10;z++) {
for (var a = 1;a<=z;a++) {
str2 = str2 + '☆';
}
str2 = str2 + '\n';
}
console.log(str2);
// for循环完成99乘法表
var str3 = '';
for (var b = 1;b <= 9;b++) {
for (var c = 1;c<=b;c++) {
str3 = str3 + c + '×' + b + '=' + b * c + '\t';
}
str3 = str3 + '\n';
}
console.log(str3);
//接收用户输入的用户名与密码,如果用户名与密码是:admin与123456,那么提示登陆成功,否则一直输入
do {
var user = prompt('用户名');
var pwd = prompt('密码');
} while (user !== 'admin' || pwd !== '123456')
alert('登录成功');
//计算1-100整数的累计和,但是跳过个位数为3的数字
var he4 = 0;
for (var d=1;d <= 100;d++){
if (d % 10 ==3 || parseInt(d % 10 ==3)) {
console.log('跳过:'+ d);
continue;
}
he4 = he4 + d;
}
console.log('1-100除却个位数为3的数字的和是:' + he4);
</script>