text: Count ways to reach a score using 1 and 2 with no consecutive 2 s | Bottom up approach for counting ways to reach a score using 1 and 2 with consecutive 2 allowed ; noOfWays [ i ] will store count for value i . 3 extra values are to take care of cornser case n = 0 ; Loop till " n + 1" to compute value for " n " ; number of ways if first run is 1 number of ways if first run is 2 and second run is 1 ; Driver code
code: function CountWays ( n ) { var noOfWays = Array ( n + 3 ) . fill ( 0 ) ; noOfWays [ 0 ] = 1 ; noOfWays [ 1 ] = 1 ; noOfWays [ 2 ] = 1 + 1 ; for ( var i = 3 ; i < n + 1 ; i ++ ) { noOfWays [ i ] = noOfWays [ i - 1 ] + noOfWays [ i - 3 ] ; } return noOfWays [ n ] ; } var n = 5 ; document . write ( CountWays ( n ) ) ;