Skip to content

Instantly share code, notes, and snippets.

@AntonisFK
Created April 6, 2016 22:10
Show Gist options
  • Save AntonisFK/7d965ea7ca080e4127027731a557ff48 to your computer and use it in GitHub Desktop.
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.
//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