freeCodeCamp Challenge Guide: Use the Rest Parameter with Function Parameters

Use the Rest Parameter with Function Parameters

Relevant LInks

Hints

Hint 1

This code

const product = (n1, n2, n3) => {
  const args = [n1, n2, n3];
  return args.reduce((a, b) => a * b, 1);
}
console.log(product(2, 4, 6)); //48

Can be written as such

const product = (...n) => {
  return n.reduce((a, b) => a * b, 1);
}
console.log(product(2, 4, 6)); //48

Solutions

Solution 1 (Click to Show/Hide)
const sum = (...args) => {
  return args.reduce((a, b) => a + b, 0);
}
console.log(sum(1, 2, 3)); // 6
26 Likes