Rest operator challenge problem

I’m confused. The one test that I cannot get it to pass is:

“The sum function uses the … spread operator on the args parameter.”

I find this confusing. I’ve used the spread operator in both the arg variable and the arguments for the return statement. I have tried putting it other places in the code, but that seems to ruin the whole thing when I do that.


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

Thank you! Now, I understand.

This does clarify the task, but that only goes to show that the task needs adjustment. Line 4 should be removed entirely. It only obfuscates the code changes needed.

Still confused. So what is the final code?

Why does this not work?

Help much appreciated

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

1 Like

Don’t worry I figured it out :slight_smile:

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