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