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];
  let total = 0;
  for (let i = 0; i < args.length; i++) {
    total += args[i];
  }
  return total;
}
console.log(product(2, 4, 6)); //48

Can be written as such

const product = (...n) => {
  let total = 0;
  for (let i = 0; i < n.length; i++) {
    total += n[i];
  }
  return total;
}
console.log(product(2, 4, 6)); //48

Solutions

Solution 1 (Click to Show/Hide)
const sum = (...args) => {
  let total = 0;
  for (let i = 0; i < args.length; i++) {
    total += args[i];
  }
  return total;
}
console.log(sum(1, 2, 3)); // 6
101 Likes