Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
Add Comment
Please, Sign In to add comment