Created
February 10, 2022 17:43
-
-
Save dev-sampsonorson/f51f516a10aae4b1921ede990274b9a8 to your computer and use it in GitHub Desktop.
Recursive implementation to sum natural numbers
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
const input1 = 10; | |
const input2 = 1; | |
const input3 = 0; | |
const sumNaturalNumbers = (n) => { | |
if (n <= 1) | |
return n; | |
return n + sumNaturalNumbers(n - 1); | |
} | |
console.log(sumNaturalNumbers(input1)); | |
console.log(sumNaturalNumbers(input2)); | |
console.log(sumNaturalNumbers(input3)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment