JS算法练习题目

时间:2022-01-17 00:14:13

1.1到100的和

var sum = 0 ;

for(var i = 1; i<= 100; i++){
sum = sum + i;
}
console.log("从1加到100的和:" + sum);

 

2.6的阶乘

var result = 1;
for(var i = 1; i <= 6 ; i++){
result = result * i;
}
console.log("6的阶乘为:" + result);

 

3.水鲜花数

var a , b , c;//a为百位,b为十位,c为个位
for(var i = 100; i < 1000; i++){
a = parseInt(i/100);
b = parseInt((i%100)/10);
c = parseInt(i%10);
if (i == a * a * a + b * b * b + c * c * c ) {
console.log("水鲜花数为:" + i);
}
}

4 .取出四位数的每一位数

    var num = 4396;
    var a = parseInt(num/1000);(取出千位)
    var b = parseInt((num%1000)/100);(取出百位)
    var c = parseInt(((num&1000)%100)/10;(取出十位)
    var d = num%10;(取出个位)
    console.log(a,b,c,d);

5.99乘法表

var z = "";
     for(var x = 1; x <= 9; x++ ){
      for(var y = 1; y <= x ; y++){
        z += x + "*" + y + "="+ x * y + "\t";
        }
        z+="\n";
     }
    console.log(z);