Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions 04-recursion/02-unwinding/sum-up-to-solution.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
function sumUpTo(n) {
// Base case - when n is 1, we return 1
if (n === 1) {
return 1;
// Test case - when n is 0, we return 0
if (n === 1 || n === 0) {
return n;
}

// Recursive case - when n is greater than 1, we return the sum of n and sumUpTo(n - 1)
return n + sumUpTo(n - 1);
}
2 changes: 2 additions & 0 deletions 04-recursion/02-unwinding/sum-up-to-test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const sumUpTo = require("./sum-up-to");

test('Summing up positive integers', () => {
expect(sumUpTo(5)).toBe(15);
expect(sumUpTo(10)).toBe(55);
Expand Down