Skip to content

Instantly share code, notes, and snippets.

@softwarespot
Last active September 13, 2015 17:07
Show Gist options
  • Save softwarespot/5d3ac94ac18c63f9ae25 to your computer and use it in GitHub Desktop.
Save softwarespot/5d3ac94ac18c63f9ae25 to your computer and use it in GitHub Desktop.
Recursive sum function inside an IIFE. (Not my idea!) Uses ES5 syntax.
// Recursive sum function inside an IIFE. (Not my idea!)
(function () {
main();
function main() {
console.write(sum(10));
}
function sum(n) {
if (n === 1) {
return 1;
}
return n + sum(n - 1);
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment