面试题 递归算法1+2+....+100求和
var sum2 = 0;
var sum3 = 0; function calc2(num) { if (num > 0) { //递归:在函数体中自己调用自己,实现一个简易的循环 sum2 += num; num--; calc2(num); //arguments.callee(num); } return sum2; } function calc3(num) { if (num < 11) { //递归:在函数体中自己调用自己,实现一个简易的循环 sum2 += num; num++; calc3(num); //arguments.callee(num); } return sum2; }
//var result2 = calc2(10); var result2 = calc3(1);
console.log(result2); function num(n) { if (n == 1) return 1; return num(n - 1) + n; } num(100);