Use the Rest Parameter with Function Parameters

Here is the task:
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.

Here is my code:
const sum = (function() {

return function sum(…args) {

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

};

})();

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

console.log(sum(1,2,3,4));

console.log(sum(5));

console.log(sum(0));

I cant pass the task because I have this message: The sum function should use the ... rest parameter on the args parameter.

What shoud I do? How to pass it?

Your code so far


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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 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

hit the ‘reset all code button’ start over from scratch. what you wrote is not good. you dont need set sum to be iife, why?
look at the sample code, you dont have to do that much to the starting code to make it pass. just change the arguements passed to be something else and comment out the args variable on the second line.

I think you had the wrong starting code,this is the starting 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

reset your code and try again

The sum function should use the ... rest parameter on the args parameter. - I have this warning, so I put already rest parameter(…args), …I’m not sure where in that case is my mistake

you have two nested functions, you shouldn’t. reset your code and try again

the tests are checking what’s written in your code, the nested functions make the tests fail