Why Use the rest Operator with Function Parameters not working with arrow notation

Hello,
I have a question about the problem that is about using rest operators with function parameters.

I rewrote the offered code with arrow notation, but one of the tests is not passing.


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


This is my code


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

Any idea why is it not passing just that one test?

Thanks in advance

It is 3 dots “…” not horizontal ellipsis “…” (char code 8230).

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

1 Like

Thank you for doing that, I wasn’t sure how to do it.
I am sorry I haven’t used Ask for Help button, I was hoping to find answer on forum first, so I am not one of those that asks already answered question.

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

@lasjorg I have tried your version of code, still the same error.

Thank you all for responding

I haven’t improved it from the previous version

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

But I did complete challenge with different version

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

My question was why is the one with arrow notation failing last test?

How do I do the blurred part, so I don’t spoil it in future for others.

So if I assumed correctly solution with arrow notation should also be correct, but because it was outside the requirements it wasn’t covered with test?

Thank you for your answer

The tests are using the outer sum function to validate the results of your solution, so it must remain for the tests to work properly.

In general, try not to post full working solutions. All you really needed to ask in this post was why didn’t your first code not work.

1 Like