Code challenge: Stairmaster
I’m failing with this code challenge: Link to challenge
My code seems to return the correct result as described in the instructions, but it fails 1 of 5 tests. Does anyone have an idea, why?
function stairmaster(n) {
function climb(num) {
if(num < 0) return 0;
if(num === 0) return 1;
return climb(num-1) + climb(num-2) + climb(num-3);
}
return climb(n)
}
console.log(stairmaster(4));
I think you need more base cases:
climb(0)
should be 0
climb(1)
should be 1
climb(2)
should be 2
climb(3)
should be 4
details
for 1 to 3, the result should be 2n−1 since that is the number of subsets of a set of size n
2 Likes
Thanks for your answer. You are right – I forgot to test
console.log(stairmaster(0));
This passes all tests now:
function stairmaster(n) {
function climb(num) {
if(num < 0) return 0;
if(num === 0) return 1;
return climb(num-1) + climb(num-2) + climb(num-3);
}
if(n===0) return 0; // <-- needed to add this line
return climb(n);
}