for循环运用
质数求和(只被1和本身整除的数)
for(var i=2;i<=100;i++){
for(var j=2;j<i;j++){
if(i%j==0){
break;
}
}
if(i<=j){
sum + i;
}
}
9x9乘法表
document.write("<table border = '1' background = 'red'>");
for(var i = 1;i<=9;i++){
document.write("<tr>");
for(var j = 1;j<=i;j++){
document.write('<td>'+j+"x"+i+"="+i*j+'</td>');
}
document.write("</tr>");
}
document.write("</table>");
斐波那契数列
<script>
// 2 3 5 8 13 21 34 55 89 144 233 377
var a = 1;
var b =1;
for(var i = 0;i<12;i++){
// var temp = b;
// b = a+b;
// a = temp;
[a,b] = [b,a+b]
console.log(b);
}
</script>
</body>
1~10000的所有完全数(除本身外的所有因数和等于本身)
<script>
for(var i =1;i<=10000;i++){
var sum = 0;
for(var j =1;j<=i;j++){
if(i%j==0&&i!=j){
sum +=j;
}
}
if(i==sum){
console.log(i);
}
}
</script>
等腰三角形
<script>
var x = prompt("请输入数字"); //弹出输入框
for(var i = 0;i<x;i++){
for(var j = i;j<x;j++){
document.write("-");
}
for(var k = 0;k<2*i+1;k++){
document.write("*");
}
for(var n = i;n<x;n++){
document.write("-");
}
document.write("</br>");
}
</script>
continue和break
<script>
//continue跳出继续循环
var sum1 = 0;
for(var i = 2;i<100;i++){
if (i%2==0&&i!=2) {
continue;
}else if(i%3==0&&i!=3){
continue;
}else if(i%5==0&&i!=5){
continue
}else if(i%7==0&&i!=7){
continue;
}else{
sum1 +=i;
}
}
console.log(sum1);
//break中断循环
var sum2 = 0;
for(var i=2;i<=100;i++){
for(var j=2;j<i;j++){
if(i%j==0){
break;
}
}
if(i<=j){
sum2+=i;
}
}
console.log(sum2);
</script>
for循环求水仙花数
<script>
// var a = Number(prompt("输入一个三位数"));
for(var a = 100;a<1000;a++){
var i = parseInt(a/100);
var j = parseInt(a%100/10);
var k = parseInt(a%10);
if (Math.pow(i,3)+Math.pow(j,3)+Math.pow(k,3) == a) {
console.log(a);
}
}
// Math.pow(2,3) //2的3次方
</script>