Use the Rest Operator with Function Parameters. Please what am I missing

I can’t find what I’m missing

Your code so far


const sum = (function() {
  "use strict";
  return function sum(...z) {
    const args = [ ...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/75.0.3770.142 Safari/537.36.

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

1 Like

You’re failing exactly one test. They’re checking the parameter name being accepted by your function, to make sure that parameter is named args. Your parameter is named z, which you then spread, and create a new array, which isn’t neccessary.

Instead, put args in the parameters of your function, just like you have z, remove the definition for args (where you set it up as a clone of z), and you should be good.

Please can you explain more…I have tried your initial explanation but it still didn`t work…thanks

So I took your code, above, and commented it. There is no reason for ‘z’, and that is what is causing your code to fail. That. Just that. If you simply remove the line

const args = [...z];

and used return function(...args){, you’d have the same exact result, without an unneccesary variable, and you’d pass.

Short answer, do it without the ‘z’.

I got it now…is bad it took me so long to get this…:pensive:

1 Like

Not bad at all.

As a developer, and an all-around old-school techie, I find that I learn best by breaking things. When I’ve got them good and busted, figuring out WHY they’re busted, and how to fix them? That’s when I learn the most.

You busted this one good, and hopefully you learned a little something. The lesson here is, don’t create ‘throwaway’ variables. If you create a function parameter solely for the purpose of assigning it to a variable and never use it again… do you really need one or the other?

It was a very good question you asked, and it was an easy gotcha. Glad it worked out, and don’t get discouraged. You’re doing fine. :wink:

1 Like

Lol…yeah it really made me think hard…thanks for the encouragement too