# JS Interview Prep - Recursion

This post was originally published on Tech.io. Their platform provides runnable code snippets so you can run the answer code live in the browser!

### Recursion in Javascript

This post goes through code samples utilizing recursion. We will state the problems and then provide runnable code snippets that show the answer. The answers will be hidden you can try them on your own and then peek.

Special thanks to the Rithm School free computer science course and their exercises on github

### Question 1: Sum all numbers

Write a function called `sumRange`

. It will take a number and return
the sum of all numbers from 1 up to the number passed in.

**Sample:**
sumRange(3) returns 6, since 1 + 2 + 3 = 6.

## Answer 1

The most important thing we need for recursive solutions is a base case. There needs to be a way of exiting the loop or the function will go on forever. The base case in the below code code is that when the input is 1, return 1. Eventually the function will return 6, the correct answer Once you understand this code move on to the next problem1 | var output = sumRange(3) |

### Question 2: Power function

Write a function called `power`

which takes in a base and an exponent. If
the exponent is 0, return 1.

**Sample:**

1 | console.log(power(2, 4)); // 16 |

## Answer 2

You can think of it in terms of this example:1 | 2^4 = 2 * 2^3; |

1 | console.log(power(2, 4)); // 16 |

### Question 3: Calculate factorial

Write a function that returns the `factorial`

of a number. As a quick refresher,
a factorial of a number is the result of that number multiplied by the number
before it, and the number before that number, and so on, until you reach 1.
The factorial of 1 is just 1.

**Sample:**

1 | factorial(5); // 5 * 4 * 3 * 2 * 1 === 120 |

## Answer 3

1 | console.log(factorial(5)); // 120 |

### Question 4: Check all values in an array

Write a function called `all`

which accepts an array and a callback and returns
true if every value in the array returns true when passed as parameter to the
callback function

**Sample:**

1 | var allAreLessThanSeven = all([1,2,9], function(num){ |

## Answer 4

1 | var allAreLessThanSeven = all([1,2,9], function(num){ |

### Question 5: Product of an array

Write a function called productOfArray which takes in an array of numbers and returns the product of them all

**Sample:**

1 | var six = productOfArray([1,2,3]) // 6 |

## Answer 5

1 | var six = productOfArray([1,2,3]) // 6 |

### Question 6: Search JS object

Write a function called `contains`

that searches for a value in a nested object.
It returns true if the object contains that value.

**Sample:**

1 | var nestedObject = { |

## Answer 6

1 | var nestedObject = { |

### Question 7: Parse a multi-dimensional array

Given a multi-dimensional integer array, return the total number of integers stored inside this array

**Sample:**

1 | var seven = totalIntegers([[[5], 3], 0, 2, ['foo'], [], [4, [5, 6]]]); // 7 |

## Answer 7

1 | var seven = totalIntegers([[[5], 3], 0, 2, ['foo'], [], [4, [5, 6]]]); // 7 |

### Question 8:

Write a function that sums squares of numbers in list that may contain more lists

**Sample:**

1 | var l = [1,2,3]; |

## Answer 8

1 | var l = [1,2,3]; |

### Question 9:

The function should return an array containing repetitions of the number argument. For instance, replicate(3, 5) should return [5,5,5]. If the times argument is negative, return an empty array.

**Sample:**

1 | console.log(replicate(3, 5)) // [5, 5, 5] |

## Answer 9

1 | console.log(replicate(3, 5)) // [5, 5, 5] |