text: Count ways to reach the nth stair using step 1 , 2 or 3 | A recursive function used by countWays ; Driver Code
code: function countWays ( n ) { let res = new Array ( n + 2 ) ; res [ 0 ] = 1 ; res [ 1 ] = 1 ; res [ 2 ] = 2 ; for ( let i = 3 ; i <= n ; i ++ ) res [ i ] = res [ i - 1 ] + res [ i - 2 ] + res [ i - 3 ] ; return res [ n ] ; } let n = 4 ; document . write ( countWays ( n ) ) ;