Using the Rest Operator with Function Parameters

Tell us what’s happening:

Hello guys, Please help with this. Am not getting it

Your code so far


const sum = (function() {
  "use strict";
  return function sum(...args) {
    const args = [ x, y, z ];
    return args.reduce((a, b) => a + b, 0);
  };
})();
console.log(sum(1, 2, 3)); // 6

Your browser information:

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

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/es6/use-the-rest-operator-with-function-parameters

What do the failing tests say?

TypeError: unknown: Duplicate declaration “args”

args is declared when you write sum(...args)
Then when you write const args = [ x, y, z ]; you are attempting to redeclare something that already exists.

args is defined within the parameters , so you would not want to redefine args again. Instead, you want to declare 3 variables (x, y, and z) by destructuring args.

Ok, but then where does the rest operator come in

Here:

sum(...args)

The three dots before args.

That is exactly what i did, …args

Yes, but now you don’t need the extra line defining args.

Sorry for the confusion in my earlier reply about needing to destructure args. I only half read the instructions.

Ok, now i understand. Thought i wasnt supposed to touch their own code. Thanks a lot, you have been really helpful