JSFiddle - React, Tailwind, and code Playground

by nerdycap007

JavaScript

/* n steps in a stair
1 or 2 steps at a time
No. of ways he can reach top of stairs */

n /* = 5
5
3 = 1, 1, 1, 1, 1
3 = 1, 2 ==> 1 * 2 + 1 = 3
5 = 
2, 2, 1 - 3
2, 1, 1, 1 - 4 
1, 1, 1, 1, 1 - 1



4/2 = 2


n-2 n-1
occurrence of 2s * 2
 */
 
function getWays(n, count) {
	
  if (n === 0) {
   	getWays(n, count + 1)
  } else {
  	getWays(n-2)
  	getWays(n-1)
  } 
  
  return count
}

console.log(getWays(3, 0))