Created
May 24, 2019 20:50
-
-
Save jcunanan05/9cc2256748aa625d42708e7e9b7106fa to your computer and use it in GitHub Desktop.
stair case problem on how many steps it can take
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
function staircase(stairSteps) { | |
function fibonnaci(n) { | |
if(n <= 1) return 1; | |
else { | |
return fibonnaci(n - 1) + fibonnaci(n - 2); | |
} | |
} | |
return fibonnaci(stairSteps); | |
} | |
/** | |
* | |
* @param {Number} stairSteps | |
* @param {Number[]} stepList | |
*/ | |
function variableStepsStaircase(stairSteps, stepList) { | |
function countSteps(step) { | |
if (step > stairSteps) return 0; | |
if (step === stairSteps) return 1; | |
if (step <= 1) return 1; | |
return countSteps(stairSteps - step) | |
} | |
return stepList.reduce((accumulator, currentValue) => accumulator + countSteps(currentValue)) | |
} | |
// test cases | |
// const staircase = require('../staircase') | |
// describe('staircase', () => { | |
// it('base case returns 1', () => { | |
// expect(staircase(-1)).toBe(1) | |
// }) | |
// it('n1 returns 1', () => { | |
// expect(staircase(1)).toBe(1) | |
// }) | |
// it('n2 returns 2', () => { | |
// expect(staircase(2)).toBe(2) | |
// }) | |
// it('n3 returns 3', () => { | |
// expect(staircase(3)).toBe(3) | |
// }) | |
// it('n4 returns 5', () => { | |
// expect(staircase(4)).toBe(5) | |
// }) | |
// it('n5 returns 8', () => { | |
// expect(staircase(5)).toBe(8) | |
// }) | |
// }) | |
exports.variableStepsStaircase = variableStepsStaircase; | |
module.exports = staircase |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment