This community-built FAQ covers the “Stairmaster” code challenge in JavaScript. You can find that challenge here, or pick any challenge you like from our list.

##
Top Discussions on the JavaScript challenge *Stairmaster*

There are currently no frequently asked questions or top answers associated with this challenge – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this challenge. **Ask a question or post a solution by clicking ***reply* () below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this challenge, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

##
Join the Discussion. Help a fellow learner on their journey.

**Ask or answer a question** about this exercise by clicking **reply** () below!

You can also find further discussion and get answers to your questions over in #get-help.

Agree with a comment or answer? **Like** () to up-vote the contribution!

Need **broader help or resources**? Head to #get-help and #community:tips-and-resources. If you are wanting feedback or inspiration for a project, check out #project.

Looking for motivation to keep learning? Join our **wider discussions** in #community

**Learn more** about how to use this guide.

Found a **bug**? Report it online, or post in #community:Codecademy-Bug-Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

I took this challenge to the next level by **listing all the ways in an array** instead of just the number of ways. When we have all the permutations, we can just get the number of ways by returning the length of the array. Furthermore, to pass this test, we have to define the case when `n === 0`

.

I wrote a function called` theseAddToSum`

that takes in an array of `steps`

, such as `[1, 2, 3]`

, and a `sum`

, here is `4`

— using **recursion**. I sketched it out on paper.

##
And here is my `theseAddToSum` function

```
function theseAddToSum(steps = [], sum) {
if (sum < 0) return []; // No solution
if (sum === 0) return [[]]; // A solution
let results = [];
if (steps.length < 1) return 'error';
for (let i = 0; i < steps.length; i++) {
let cur = steps[i];
let remaining = sum - cur;
let c = theseAddToSum(steps, remaining);
for (let solution of c) {
solution.push(cur);
results.push(solution);
}
}
return results;
};
```

This is the solution that helped me pass this challenge:

function theseAddToSum (steps = [], sum) {
// Base cases
if (sum < 0) return []; // No Solution
if (sum === 0) return [[]]; // A Solution
let results = [];
if (steps.length < 1) return 'error';
for (let i = 0; i < steps.length; i++) {
let cur = steps[i];
let remaining = sum - cur;
let c = theseAddToSum(steps, remaining);
for (let solution of c) {
solution.push(cur);
results.push(solution);
}
}
return results;
}
function stairmaster(n) {
// Base cases
if (n === 0) return 0;
let ways = theseAddToSum([1, 2, 3], n);
console.log(ways);
return ways.length;
}
console.log(stairmaster(4));
// Leave this so we can test your code:
module.exports = stairmaster;

3 Likes

```
function stairmaster(n) {
let count = 0;
function step(stairs) {
for (let i = 1; i <= 3 && i <= n; i++) {
if (stairs + i > n) {
break;
} else if (stairs + i === n) {
count++;
break;
} else {
step(stairs+i);
}
}
}
step(0);
return count;
}
```

I did the same thing as the other posts, I used **recursion**.

##
recursive version

```
function stairmaster(n) {
const maxSteps = 3;
if (n <= 0) { return 0; }
else if (n <= maxSteps) { // base case
return Math.pow(2, n - 1);
}
else { // recursive case
let total = 0;
for (let j = 1; j <= maxSteps; j++) {
total += stairmaster(n - j);
}
return total;
}
}
```

But I guess storing the values already computed would be useful if the number of stairs is big.

##
version using memoziation

```
const mastered = [0, 1, 2, 4];
function stairmaster(n) {
const maxSteps = 3;
if (n <= 0) { return 0; }
else if (n < mastered.length) { // memoization used
return mastered[n];
}
else { // recursive case
let total = 0;
for (let j = 1; j <= maxSteps; j++) {
total += stairmaster(n - j);
}
mastered[n] = total;
}
return mastered[n];
}
```

Very fast calculation with combinations, pemutations, factorial and caching.

```
function cachedFactorial() {
let cache = new Map();
return function fact(n) {
if (cache.has(n)) return cache.get(n);
let res = n ? n * fact(n - 1) : 1;
cache.set(n, res);
return res;
};
}
const factorial = cachedFactorial();
function stairmaster(n) {
if (n === 0) return 0;
let result = 0;
for (let i = 0; i <= n; i++) {
for (let j = 0; j <= Math.floor(n / 2); j++) {
for (let k = 0; k <= Math.floor(n / 3); k++) {
if (i + j * 2 + k * 3 === n) {
const permutations = factorial(i + j + k) / (factorial(i) * factorial(j) * factorial(k));
result += permutations;
}
}
}
}
return result;
}
```

1 Like