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

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

#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


#2

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

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


#3

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.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.

Can you provide a link to the challenge?

In the future, try using the Ask for Help button, so that your code, the challenge url, and other information gets automatically included in the post.

Thank you.


#4

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


#5

The Ask for Help creates a thread on the forum for you. It is the same as creating your own thread on the forum, but we also get lots of additional information automatically.


#6

Can you please post your latest code (all of it)? Make sure to use the 3 backticks on the line before and after the code you post.


#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?


#8

The instructions only want your to modify the internal sum function which is part of the IIFE (Immediately Invoked Function Expression) whose result gets assigned to the outer sum variable.

How are you supposed to know only to modify the internal sum function? Because, the instructions tell you to modify the function sum to use the rest operator so it works in the same way with any number of parameters. If you notice, only the internal function has any parameters listed. Also, inside the inner sum function is where they have to use destructuring to get the argument values. By using the rest operator on the inner sum function parameter section, the destructuring line is not necessary (as you already seem to know).

FYI - 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.


#9

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


#10

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.