From 5cea2b63133ba0a06d4adc001f34332135e1c0c5 Mon Sep 17 00:00:00 2001 From: Kamilla Date: Sat, 2 Jul 2016 13:34:41 -0400 Subject: [PATCH] problem 9.1 --- .../9.1-HoppingSteps.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 Chapter9-Recursion&Dynamic Programming/9.1-HoppingSteps.js diff --git a/Chapter9-Recursion&Dynamic Programming/9.1-HoppingSteps.js b/Chapter9-Recursion&Dynamic Programming/9.1-HoppingSteps.js new file mode 100644 index 0000000..b9be426 --- /dev/null +++ b/Chapter9-Recursion&Dynamic Programming/9.1-HoppingSteps.js @@ -0,0 +1,16 @@ +/* +Problem: A child is running up a staircase with n steps, and can hop either 1 step, 2 steps, or 3 steps at a time. Implement a method to count how many possible ways the child can run up the stairs. +*/ + +/** ex: 4 steps should return 7 ways: +1,1,1,1; 2,2; 2,1,1; 1,2,1; 1,1,2; 1,3; 3,1 +**/ + +function numberOfWays(steps) { + if (steps < 0) return 0; + else if (steps === 0) return 1; + //num of steps will equal num of steps to previous step plus one + else { + return numberOfWays(steps - 1) + numberOfWays(steps - 2) + numberOfWays(steps - 3); + } +}