Help with Rest parameter

Tell us what’s happening:
Describe your issue in detail here.
I’m having trouble grasping this rest parameter concept and why it isn’t working for me. I’m confused because the instructions say to , " 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". But the error message says " sum should be an arrow function which uses the rest parameter syntax on the args parameter" I’ve tried using the rest paremeter syntax on sum function and tried it on the args variable and nothing worked. What am I missing? I’ve tried referencing MDN and Eloquent JS book and nothing is helping understand why this isn’t working for me.

  **Your code so far**

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

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36

Challenge: Use the Rest Parameter with Function Parameters

Link to the challenge:

Hi, make sure to reset your code, when you try to apply different solution.
You have a trailing comma in your function parameters field, which breaks the compiler- (...x,). This is incorrect JS syntax.

Maybe to understand it better, you should play a bit with the code and monitor how it behaves. Try to call the function you work with, with few parameters. For example sum(2 ,4 , 7); use it after the function is declared. Place a log in its body, to see the parameters you get. Initially the sum function takes 3 parameters, x, y, z. If you use console.log(x,y,z), while you use the function with the values i suggested, you will see in the console 2, 4 and 7. Then try to use the pattern shown in the lesson example, where the parameters are not (x, y, z) but (...args). Keep in mind saying “args” is optional, you can name that parameter however you like, but in case you call it args, you will want to remvoe the line in the function, where the same name is used again to declare a variable- const args = [x, y, z]; . After that you can try again to log your arguments, this time you want to do it as- console.log(args). If you performed the steps correctly, you will notice you get the same arguments you fed the function- 2, 4, 7, but inside an array. This is because thanks to the spread operator, the “args” argument, or however you called it, consumes all arguments you use to call the function and places them in an array. If you were to use sum as sum(2,5), the args would be [2, 5], if you would use sum(5, 6, 2, 4), you will see [5, 6, 2, 4] in the console. This is especially useful, as you can design a function which can work with varying number of arguments. Also, arrays are extremely comfortable to work with, as they provide some very powerful methods like reduce, which is used in the example. For now, dont worry about how it works.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.