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

The tests are not expecting you to change the anonymous function to a named function nor are the tests expecting you to change the original arrow function syntax.

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?

Show the code you tried.

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.

Think about what the ... does when you put it in front of a parameter name. The rest parameter can only be used on the last parameter in a function definition, so the following would not be valid syntax:

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

Look at the example to see how they create an array of arguments using the ... in the function definition.

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.

You are not understanding what ...args represents. I can pass 100 arguments to the example howMany function and that syntax will create an array named args with 100 elements, with each element being the value an argument passed to the function.

The above would create a variable named x containing the value of the first argument passed to the function, a variable named y containing the value of the second argument passed to the function, and a variable named z which would be an array of any other arguments after the first two passed to the function.

Then why would (…arg) be wrong here?

Look at the return statement and see if you see a variable named arg.

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.

If you write (...arg) in the function definition, arg is a parameter which is a variable local to the function. The point is, you should not be trying to create a parameter named arg for this challenge.

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.

It most certainly is. Look at the above function definition. It is a parameter of the function. By the very definition of a parameter, it is a variable local to the function.

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.

You are supposed to get rid of the existing line which declares args and using a rest parameter instead within the function. That is the entire point of the exercise.

Do you understand if I create a function (like below), that the parameter theParameter is a variable local to the function that can be referenced inside that function?

function myFunction(theParameter) {
  return "The value of my function's parameter is " + theParameter;
}

console.log(myFunction(10)); // displays The value of my function's parameter is 10

All function parameters are declared variables as soon as you write them as parameters.

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?

Look at my first reply.

I suggest you click the Reset Challenge and not try to change anything except the function parameters.

I don’t understand your first reply.