ES6 - Use the Rest Parameter with Function Parameters

Tell us what’s happening:
Describe your issue in detail here.

Your code so far

const 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 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36

Challenge: ES6 - Use the Rest Parameter with Function Parameters

Link to the challenge:

so I spent 2 hours trying to figure this challenge out. then I gave up and checked the forum, then the Hint, then the solution. The code above is the solution given on the site, i did not write it.
The forum and the Hint used functions that have not been taught yet on FreeCodeCamp. the reduce().

Did I skip some lessons?
I completed every previous lesson twice (I like to finish a lesson than redo it a week later so i remember it better).

please help.

i am unsure if I have understood the question. Do you mean you did not know about reduce? (or if not that one, which function did you have a question about?)

This is the starting code:

const sum = (x, y, z) => {
  const args = [x, y, z];
  return args.reduce((a, b) => a + b, 0);
}

The reduce was already there. The challenge was asking you to change the first two line here.

I know, but I dont know what reduce means, so I cant modify a code without know what it means… i did look it up, but I want to make sure that im not missing anything.

yes, it is used in the example, but it was never taught.

and the solution* used it as well

You shouldn’t modify the line with reduce at all. You were not asked to change that line. You should only change the function argument list.

yes true. I understand that it definitely would look weird. I am glad you looked it up.
I think it is a good instinct to look up any functions you do not understand before you modify the code.

1 Like

If you write it this way it might be more clear:

const cumulatedValue = array.reduce((previousValue, currentValue)=>previousValue+currentValue, 0);

The reduce function returns a cumulated value that is the result of running the callback on each member of the array, starting with the start value (second argument of the reduce function). The reduce callback function takes two arguments, representing the previousValue and
the currentValue.

For an array [1, 2, 3], here are the callback operations in order given the reduce function above:
0 + 1 = 1
1 + 2 = 3
3 + 3 = 6

Each operation is previousValue + currentValue, as specified in the callback.

well actually no.
usually they have commented lines telling you where or where not to change code. and in this lesson there were no lines.
the instructions say:
" Modify the function sum using the rest parameter in such a way that the function sum is able to take any number of arguments and return their sum."

and thats all besided the point of my question, which was: Did i miss any lessons on special functions.?

Well actually yes. You should not make any modification to the line with reduce. The instructions only talk about modifying the inputs.

The instructions say

Modify the function sum using the rest parameter in such a way that the function sum is able to take any number of arguments and return their sum.

This is the original function:

const sum = (x, y, z) => {
  const args = [x, y, z];
  return args.reduce((a, b) => a + b, 0);
}

Here is the example given for using a rest parameter:

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

So, the “rest parameter” is the part (...args). So, the solution needs that piece in the argument list:

const sum = (...args) => {
  const args = [x, y, z];
  return args.reduce((a, b) => a + b, 0);
}

Oh, but now we have declared args twice, so we should fix that:

const sum = (...args) => {
  return args.reduce((a, b) => a + b, 0);
}

Ah ha, the solution has been reached without doing a single thing to the line with reduce, consistent with the instructions.

Is it a bit weird that there is a function you haven’t seen before in the seed code? Sure. Do you need to do anything with it? No.

well, the thing is in order to know how to change the code in 1 function( Modify the function sum), i thought it would be necessary to know what the ‘return’ statement is saying, because it uses variables that aren’t anywhere else in the function a,b,c. otherwise its just guessing. I understand what your saying by the way, thank you.

Just wanna say again. Your instincts (to look up what you don’t recognize) are not wrong. If you ever are asked to modify code and you do not understand it, definitely spend the time to do that first.