
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Recursive Staircase problem in JavaScript
Suppose we have the following problem −
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 or 2 stairs at a time. We are required to count the number of ways, the person can reach the top.
We are required to write a JavaScript function that takes in a number n that denotes the number of stairs. The function should count and return the number of ways in which the stairs can be climbed.
Example
Following is the code −
const recursiveStaircase = (num = 10) => { if (num <= 0) { return 0; } const steps = [1, 2]; if (num <= 2) { return steps[num - 1]; } for (let currentStep = 3; currentStep <= num; currentStep += 1) { [steps[0], steps[1]] = [steps[1], steps[0] + steps[1]]; } return steps[1]; }; console.log(recursiveStaircase()); console.log(recursiveStaircase(4)); console.log(recursiveStaircase(13));
Output
Following is the output on console −
89 5 377
- Related Articles
- JavaScript Quicksort recursive
- Recursive multiplication in array - JavaScript
- Snail Trail Problem in JavaScript
- Distributing Bananas Problem in JavaScript
- Recursion problem Snail Trail in JavaScript
- Expressive words problem case in JavaScript
- 2 Key keyboard problem in JavaScript
- Meeting Rooms 2 problem in JavaScript
- Crack Alphabets fight problem in JavaScript
- JavaScript code for recursive Fibonacci series
- Recursive product of summed digits JavaScript
- Recursive string parsing into object - JavaScript
- Combination sum problem using JavaScript
- Return correct value from recursive indexOf in JavaScript?
- The algorithm problem - Backtracing pattern in JavaScript

Advertisements