Problem 18

Build an array with a given value Recursion Challenge

Given a value and a length, return an array of the specified length where every element is the given value.

Function Signature

buildList(value, length)

Parameters

  • value — any value (number, string, boolean, object, array, undefined, NaN, etc.).
  • length — a non-negative integer specifying the number of elements.

Output

Return an array of the given length filled with the given value.

Constraints

  • The function must use recursion.
  • The function should accept exactly two arguments.

Examples

buildList(0, 5)          → [0, 0, 0, 0, 0]
buildList('banana', 3)   → ['banana', 'banana', 'banana']
buildList(NaN, 4)        → [NaN, NaN, NaN, NaN]
buildList(true, 3)       → [true, true, true]
buildList(undefined, 1)  → [undefined]
buildList([], 2)         → [[], []]