ES6: Use the Rest Operator with Function Parameters stuck

Tell us what’s happening:
I keep getting caught on the very last objective where I am to use the ... rest parameter on the args parameter. What do I need to do to pass that test?

Your code so far


const sum=function sum(...args){
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 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36.

Challenge: Use the Rest Parameter with Function Parameters

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

1 Like

I’ve tried adding the rest parameter to the original provided code by adding ... to x, y, z when the function is first being described. It does not work. Could you point me in the correct direction in more detail?

Please don’t laugh, but my first thought was very simplistic and that was just to add ... to the first variable described as shown below.

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

I’ve also attempted adding ... before each of the x, y and z when sum is first defined and also when args is first defined. The code that I ultimately shared at the start of my post was the only one that could pass all the tests except the last one.

The trouble is, the example only lists one parameter in the initial definition of the function ...args. I don’t really know how to translate that onto the problem presented. Am I supposed to leave (x, y, z) alone in the solution?

I have also tried (x, y, …z) and that didn’t seem to work either.

1 Like

Then why would (…arg) be wrong here?

but arg isn’t a variable. It’s a parameter isn’t it?
In the example, ...arg isn’t defined as a variable either. So it’s quite confusing.

Still confused.

function howMany(...args) {
  return "You have passed " + args.length + " arguments.";
}
console.log(howMany(0, 1, 2)); // You have passed 3 arguments.
console.log(howMany("string", null, [1, 2, 3], { })); // You have passed 4 arguments.

args is never defined here.

What’s the difference between the example code and mine?

I don’t see any const args= or var args= in the example code, but I think you’re implying that that part needs to be in mine despite it.

I don’t understand your original correction then. If I don’t need to separately declare args as a variable, then what is the problem with the original code?

I don’t understand your first reply.

so the very first line is what I understand to be the function parameters.

const sum = (x, y, z) =>

I am to only change the x, y and z of this line?

It’s the format that’s shown in the “Get a hint” link. It’s the firs ttime where the solution shown is not the one that works.

Okay, finally got it.

const sum = (…args) => {

return args.reduce((a, b) => a + b, 0);

}

console.log(sum(1, 2, 3)); // 6

it works on the site. Is this what you were expecting as well?

1 Like

Okay, thank you very much for your help!

Thank you very much for this!