luffee
September 15, 2018, 3:49pm
1
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
lasjorg
September 15, 2018, 4:39pm
2
It is 3 dots “…” not horizontal ellipsis “…” (char code 8230).
const sum = (...args) => args.reduce((a, b) => a + b, 0);
1 Like
luffee
September 15, 2018, 5:03pm
4
camperextraordinaire:
I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make easier to read.
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
luffee
September 15, 2018, 5:12pm
7
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?
luffee
September 15, 2018, 5:40pm
9
camperextraordinaire:
Your code has been blurred out to avoid spoiling a full working solution for other campers who may not yet want to see a complete solution.
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