欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

JS基础语法---函数练习part1---5个练习

程序员文章站 2022-05-18 20:58:22
练习1:求两个数字的和:获取任意的两个数字的和 function getSum(x, y) { return x + y; } console.log(getSum(10, 20)); 练习2:求1-100之间所有的数字的和 function geteverySum() { var sum = 0; ......

练习1:求两个数字的和:获取任意的两个数字的和

      function getsum(x, y) {
        return x + y;
      }
      console.log(getsum(10, 20));

 

练习2:求1-100之间所有的数字的和

      function geteverysum() {
        var sum = 0;
        for (var i = 1; i <= 100; i++) {
          sum += i;
        }
        return sum;
      }
      console.log(geteverysum());

 

练习3:求1-n之间所有数的和

      function geteverysum(n) {
        var sum = 0;
        for (var i = 0; i <= n; i++) {
          sum += i;
        }
        return sum;
      }
      console.log(geteverysum(89));

 

练习4:求n-m之间所有数的和

      function geteverysum2(n,m) {
        var sum = 0;
        for (var i = n; i <= m; i++) {
          sum += i;
        }
        return sum;
      }
      console.log(geteverysum2(32,90));

 

练习5:求圆的面积

      function geteverysum2(n,m) {
        var sum = 0;
        for (var i = n; i <= m; i++) {
          sum += i;
        }
        return sum;
      }
      console.log(geteverysum2(32,90));