JavaScript中变量练习题

  1. 计算2的n次幂,n可输入,n为自然数
    规律:12
    1
    22
    1
    222
    var n = parseInt(window.prompt(‘input’));
    var mul = 1;
    for (var i = 0; i < n; i ++) {
    mul *= 2;
    }
    document.write(mul)

    1. 计算n的阶乘,n可输入
      5 = 543211
      4 = 4
      3211
      3 = 3
      211
      var n = parseInt(window.prompt(‘input’));
      var mul = 1;
      for (i = 1;i <= n;i++){
      mul *= i;
      }
      document.write(mul)

    2. 输入a,b,c,三个数打印出最大的
      var a = parseInt(window.prompt(‘input’));
      var b = parseInt(window.prompt(‘input’));
      var c = parseInt(window.prompt(‘input’));
      if(a > b){
      if (a > c) {
      document.write(a);
      }else{
      document.write©;
      }
      }else{
      if (b > c){
      document.write(b);
      }else{
      document.write©;
      }
      }

      1. 编写一个程序,输入三位数的正整数,输出时反向输出。如:123,321

      var n = parseInt(window.prompt(‘input’));
      var a,b,c;
      a = n % 10;
      b = (n % 100 - a) / 10;
      c = (n - n % 100) /100
      var mul = a * 100 + b * 10 + c;
      document.write(mul)

      var n = parseInt(prompt(‘输入一个三位数’));
      var a,
      b,
      c;
      a = (n - (n % 100)) / 100; //取百位
      c = n % 10; //取各位
      b = (n % 100 - c) / 10; //取十位

       var m = c * 100 + b * 10 + a;
       document.write(m);
      

      456 654
      a 456%10=6
      b 456%100=56 56-6
      c 456-(456%100)
      m=6*100+50+4
      var n = parseInt(window.prompt(‘input’))
      var a,
      b,
      c;
      a = (n - (n % 100)) / 100;
      c = n % 10;
      b = (n % 10 - c) / 10;
      var mul = c * 100 + b * 10 + a;
      document.write(mul);

      1. 著名的斐波那契数列,1 1 2 3 5 8输出第n项
        var n = parseInt(window.prompt(‘input’));
        // 1 1 2 3 5 8
        // f s t
        var first = 1;
        var second = 1;
        var third;
        if (n > 2){
        for (i = 0;i < n-2;i ++){
        third = first + second;
        first = second;
        second = third;
        }
        document.write(third)
        }else{
        document.write(1)
        }

      2. 打印100以内的质数。
        var count = 0;
        for (var i = 1;i < 100;i++){
        for (var j = 1;j <= i;j++){
        if (i % j == 0){
        count++;
        }
        }if (count == 2){
        document.write(i+" “);
        }
        count = 0;
        }
        // 另一种方法Math.sqrt()开方数
        var count = 0;
        for (var i = 2;i < 100;i++){
        for (var j = 1;j <= Math.sqrt(1);j++){
        if (i % j == 0){
        count++;
        }
        }if (count == 1){
        document.write(i+” ");
        }
        count = 0;

      var count = 0;
      for(i = 1;i < 100;i++){
      for(j = 1;j <= i;j++){
      if(i % j == 0){
      count ++
      }
      }
      if(count == 2){
      document.write(i)
      }
      count = 0;
      }
      var i = 0;
      while(1){
      i ++;
      console.log(i);
      if(i > 100){
      break;
      }
      }

你可能感兴趣的:(JavaScript)