Skip to content

Instantly share code, notes, and snippets.

@tuvo1106
Created November 20, 2018 20:16
Show Gist options
  • Save tuvo1106/1e86fabf7495e4633b1b8e85acb95396 to your computer and use it in GitHub Desktop.
Save tuvo1106/1e86fabf7495e4633b1b8e85acb95396 to your computer and use it in GitHub Desktop.
const addWithRecursion = x => {
// base case
return x <= 0
// ends function when x hits 0
? 0
// checks if x divides evenly by 2
: x % 2 === 0
// retains x and adds it to the function call of x - 1
? x + addWithRecursion(x - 1)
// ignores x and moves on to the function call of x - 1
: addWithRecursion(x - 1)
}
console.log(addWithRecursion(100)) // 2550
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment