Use the Rest Operator with Function Parameters ? The sum function uses the ... spread operator on the args parameter

Tell us what’s happening:

Your code so far


const sum = (function() {
  "use strict";
  return function sum(...x) {
    //const args = [ x, y, z ];
    return x.reduce((a, b) => a + b, 0);
  };
})();
console.log(sum(1, 2, 3)); // 6

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:63.0) Gecko/20100101 Firefox/63.0.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/es6/use-the-rest-operator-with-function-parameters

That appears to be a valid solution to me.

Looks like you might be using Firefox browser. You might try Chrome browser- the FCC tests are only guaranteed to work in Chrome.

I tried it on Chrome, but the same result.

And what result is that? You post your code, but you don’t say what the problem you’re having might be. Works fine here, on chrome and firefox.

Apologies for the confusion. When I run the code in Chrome or Firefox, it gives some kind of warning . See below
The sum function uses the … spread operator on the args parameter
Perhaps the compiler is not liking this somehow; i dont know why it does not cause the same issue for you.

I can’t imagine that it wants the variable name to be args instead of x – have you tried that? I’m on a public terminal atm, that blocks jQuery, so I am testing code on jsfiddle. And there, it works fine.

1 Like

It worked on changing the variable name to args. Probably a compiler issue. Thank you :slight_smile:

1 Like

Weird. Glad it worked, though.

the code did not work for me

If you want help you need to post your code. Anyway, there are no working solutions in this thread, but there are hints to make the OP code a working solution,

Use the “Ask for help” button if you want to create a thread with your code to ask for help

The real issue with this problem is the constraint to use a named function at all. A more clear solution uses anonymous function.

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