Use the Rest Parameter with Function Parameters Guide

function sum (x,y,z,g)  {

console.log (x+y+z+g);

}

var args =  [1, 2,3,4];

sum(...args);


I do not know what I am missing to finish this challenge: https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/es6/use-the-rest-parameter-with-function-parameters

The rest parameter should be used on the function definition, not the function call (as opposed to Spread). Look at the example code and how it is used on the howMany function.

You also changed the code to where the function does not do what it is supposed to do, you still need the function to return the result of reduce. The function also has to be an arrow function (for the last test to work).

2 Likes
const sum = (...args) => {

  const args = [x, y, z];

  return args.reduce((a, b) => a+b ,0);
}
console.log(sum(1, 2, 3)); // 6

you are defining args twice
and x, y, z are now undefined

1 Like

I cannot pass the test with this code.

const sum = (…n) => {

return n.reduce((a, b) => a + b, 0);
}
console.log(sum(1, 2, 3)); // 6

I have another question: I used hits: if you use hints, Can you still learn well?

You’re absolutely right you can’t pass with that, even though it’s perfectly valid. The error message explains exactly why, too. Look closely at the original code:

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

and look at the last test on that page. The message for that test is:

The sum function should use the ... rest parameter on the args parameter.

Does your code use the args parameter?

1 Like

I solved it some 30 minutes ago!. thanks!, but I still have the question if I can use hits and I can still learn well how to code.

Absolutely can, depending on how you use those hints. Are you examining them, deconstructing them, and understanding how they work? Or are you copying them, pasting them, and moving on?

A huge part of being a professional developer is getting a sense of “where to go to find what you need to know.” For me, I keep a tab open to https://devdocs.io/ at all times. I am constantly googling, even for things I’ve worked with for years. I often get mixed up between Array.slice() and Array.splice(), and research and reference becomes vital.

But, once the research is done, I know how to write my code, my way. I don’t parrot someone else’s response, I refine it to my own needs.

1 Like

Thanks for your advice!. No, I did not copy it. I tried to understand what I have misdone before. I noticed that I was interpreting badly the code. I know that I can use hints for understanding better now.