Help with the rest operator lesson

Tell us what’s happening:

I managed to pass the lesson by adding the rest operator to the provided code, but I then also tried to simplify the code.

I can’t pass the lesson with this code as FCC says that I am not using the rest operator with args…

I get no error in the console, and the code seems to work fine when testing. I am wondering if anyone can tell me what is wrong with this code.

Cheers!

Your code so far


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

console.log(sum(0)); 

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36.

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

Your code is fine.

This is the regex used to check the submitted code:

/function\s+sum\s*\(\s*...args\s*\)\s*{/g

As you see it specifically looks for the function keyword.

1 Like

Thanks for the answer!

This was my code answer, and it passed, but it doesn’t seem like it’s actually correct/best practice/using the spread operator:

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

})();
console.log(sum(1, 2, 3)); // 6

num isn’t used for anything so delete that line, looks absolutely fine otherwise.

thanks! i spent probably 20 minutes trying different things on this one.