Codington
0 of 6 problems solved0%

Average with Rest Params

A rest parameter gathers many arguments into an array. Type the rest parameter as number[] and return a number. Decide what to return when there are no inputs.

Keep the function pure and the code short.

function sum(...xs: number[]) { return xs.reduce((a, b) => a + b, 0); }

Your Task

Compute the average of any number of inputs.

  • Implement average(...nums: number[]): number.
  • Return 0 when no numbers are provided.
  • Avoid mutation.