ES6 - Use the Rest Parameter with Function Parameters

Tell us what’s happening:
please i wish to have help

Your code so far

const sum = (x,y,z, ...arg) => {
  const args = [x, y, z];
  let total = 0;
  for (let i = 0; i < args.length; i++) {
    total += args[i];
  }
  return total;
}

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36 Edg/112.0.1722.48

Challenge: ES6 - Use the Rest Parameter with Function Parameters

Link to the challenge:

The function (as it is given to you) has three arguments, which are then put into an array with const args = [x, y, z], which can be iterated in the for loop.

However, if you replace the arguments (x, y z) in the function declaration with a spread parameter, the function can then take any number of arguments.

Doing this also creates an array-like arguments object, so you can remove the const args declaration from inside the function too, as it will be defined already, and contain as many arguments as are supplied to the function when it is invoked. Try putting console.log(args) inside your function to see this.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.