Created
April 6, 2016 22:10
-
-
Save AntonisFK/7d965ea7ca080e4127027731a557ff48 to your computer and use it in GitHub Desktop.
Implement a function iSum that behaves just like rSum but instead of using recursion to implement the solution it uses iteration.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//iSum(1) = 1 => 1 | |
// iSum(2) = 1 + 2 => 3 | |
// iSum(3) = 1 + 2 + 3 => 6 | |
// iSum(4) = 1 + 2 + 3 + 4 => 10 | |
// iSum(5) = 1 + 2 + 3 + 4 + 5 => 15 | |
var iSum = function(num){ | |
var sum = 0; | |
if(num > 0 ){ | |
for(var i=1; i<=num; i++){ | |
sum += i; | |
} | |
} | |
return sum; | |
}; | |
iSum(4); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment