What's wrong with this code (Rest Operator)?

Tell us what’s happening:

It always returns:

// running tests

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

// tests completed

I think I’m using it, I don’t understand what’s happening, even the console log brings me the correct value (6). Thank you in advance for any help.

Your code so far


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

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

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36.

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

“on the args parameter”

I don’t see one in your code, maybe that could be a reason

In this case the tests are probably searching for something written exactly as requested. You have changed instead a parameter or variable name

It’s a bug in this exercise. This code will work fine.

const sum = (function() {
  "use strict";
  return function sum(...args) {
    return args.reduce((a, b) => a + b, 0);
  };
})();

I don’t know why it’s defined as a return function in an IIFE. As it is beyond the scope of current challenge. And you also can’t call the arguments ...nums, it will error out. You have to pass them as ...args. Again, it’s a bug in this exercise. Your code is fine though.

Because the challenge is testing the code you’ve written, not just the output (if it didn’t you could pass all the tests without using any of the features FCC is trying to teach), it has to match the description pretty closely, you generally can’t change things like variable names.

It’s only on the algorithm and project sections that you have free reign.

Your’re right, your code seems to pass. :grinning:

Thank you guys! I was starting to think that I missed something.