Give it a Rest!

Tell us what’s happening:
I rewrote the function to incorporate the ‘rest’ operator in the arguments but it does not clear the test criteria for that. It meets all other criteria and obviously is used in the function. It states ‘rest operator’ in the question but ‘spread operator’ in the criteria. What gives?
Ignore the comment in the console.log as it pertains to when there were three arguments in the original code.

Your code so far


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

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:67.0) Gecko/20100101 Firefox/67.0.

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

The example they give before you use the REST operator is using an IIFE and therefore being called immediately, yours is just storing a function in a variable.

const sum = (() => {

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

try this.

Infact there would be a simpler way (didnt add spoilers before so thanks dawson)

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

The reason yours does not work is becasue its a variable storing a function rather than the function itself, I believe.

It is an arrow function(checked in the console), uses a rest operator in the arguments parentheses of the function, accepts arrays of any length and adds them together with .reduce to produce the correct result. It is also less code which I thought was the point.
My issue is that the challenge is not accepting that the rest operator exists in the function, which is definitely does.

And an arrow function is not named but the challenge asks for the function named sum and ITS parameter to use REST

Modify the function sum :white_check_mark:so that it uses the rest operator :white_check_mark: and it works in the same way with any number of parameters.:white_check_mark:

This is the challenge. I don’t see ‘ITS’ parameter mentioned, just any number of parameters.

I have not encountered IIFE functions and thank you for pointing out that that was what the initial example was. From what I can discern, an IIFE is invoked to preserve the lixical scope of the function and that output of the function would be assigned to the variable instead of the function itself.
Your suggested example is almost exactly what I had except that mine is written in ES6 syntax and my example uses ‘const’ so that the function ‘sum’ cannot be overwritten. It also has lexical scope that does not pollute the global.

To check if you are using the rest operator the tests check what you have written, as you have removed the IIFE, which was also being considered in checking what you have written, you will never pass the test like this.

Yes, it is restrictive, but there is no other way to checkif you have used rest or not

@KeithAdams64 Though not yet deployed to production all of the challenges which currently use the IIFEs have been modified to remove the IIFE from the challenge. This means once the newer version is released to production, you would be able to write your originally posted solution to solve the challenge.

For now, just pretend like the challenge seed code looks like below:

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

I guess my frustration is that the IIFE structure has nothing to do with the module, which is supposed to be about ES6. It asks you to modify the function. It does not say to modify the function ‘but’ stay within the IIFE structure.

So is the IIFE function structure obsolete now that ‘const’ and ‘let’ have solved some of the scope issues that ‘var’ caused with regard to local and lexical scope.

The reason the IIFE was used has nothing to do with const or let or var. It was related to how the tests were implemented behind the scenes for certain challenges. IIFEs are still relevant for other reasons which are beyond the scope of this particular challenge.

Thanks Randell. I am eager to know where IIFE’s are applicable. I learned a great deal in my surly defence of my logic. I hope that the collateral damage of my learning curve does not inhibit others from entering the fray.
I would much rather battle than peek at the answer.