Use the Rest Operator with Function Parameters Issue?!

What am I doing wrong here? I have read the example, tested it and read more https://github.com/getify/You-Dont-Know-JS/blob/master/es6%20%26%20beyond/ch2.md#spreadrest

and watched this:

And read other questions asked about this but I do not get 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 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.122 Safari/537.36 Vivaldi/2.3.1440.61.

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

Can you explain why this is here:

const args = [ x, y, z ];

That’s your issue, and I think you aren’t quite understanding how this should work — that line of code doesn’t make sense.

OK thanks for that, let me keep at it.

It wasn’t a rhetorical question! I can explain how to fix it, I just want to understand your thinking here because you’re the second person in the last day or so to write that exact same thing.

1 Like

Right, I think I got it.

So I just removed the

const args = [ x, y, z ];

As the args are in the arguments in the function. The ...args get all of fetches all of the arguments.

I think that is right, please correct me if wrong. This was helpful in understanding:

Cheers!

1 Like

That’s spot on!

I’ll have a watch of the video — as I say someone else asked the exact same question recently, and I’ve seen a few people over the last few months trip up on the challenge with the same mistake

1 Like

Perfect,

Thanks for that, I’m not losing my mind then ha!!!

Cheers for the help - very nice indeed :+1:

1 Like