[SOLVED] Can't pass test on the Rest Operator (ES6)

[SOLVED] Can't pass test on the Rest Operator (ES6)
0.0 0

#1

Tell us what’s happening:
I cannot pass the final test, although everything is working in JSBin and I am using rest operator. Can anyone tell me what’s wrong?

I can’t pass this story: “The sum function uses the … spread operator on the args parameter.”

Your code so far


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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.3; Win64; x64; rv:60.0) Gecko/20100101 Firefox/60.0.

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


#2

I have never seen it used like that. Maybe try “use strict”;


#3

It’s still not working :frowning:


#4

You could drop use strict and/or streamline your code by eliminating the return

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

I don’t know what the problem is here.


#5

This test was really misleading and quite stupid in my opinion. I couldn’t pass it for ages but all I had to do was comment out a line that never needed to be there in the first place. Really pointless! Who wrote this one!


#6

You have to understand that rest operator just stores function arguments to the new array called args!


#7

I also cannot pass this problem. Maybe there are some problems with FCC. I hope @QuincyLarson will fix it soon


#8

The examples are so easy to understand and then they make the challenge 100 x harder.


#9

I really think the base code is a mess in this one. I’m not even sure what’s going on with it. This is what I got when I reset it because I remembered this one being really weird:


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

Why is sum() set as a const and then calling itself recursively? I have no idea.
What is that mess of brackets and parentheses at the end? })();? Why is the entire sum variable wrapped in parentheses?

I deleted it all and rewrote it. Here’s the solution that I wrote, because I couldn’t figure out that mess:


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

Honestly, it may be best practice to erase all their code and do it yourself, anyway. Most of the challenges are fine, but I’ve run across way too many that are frustrating because the base code is awkward. Even when the challenges are fine, though, it’s really helpful to practice it from scratch as much as you can.


#10

Thank you so much ! I was also wondering about the really strange format of the code, and the parenthesis wrapping the entire sum, made me question if I had skipped a lesson some where! thanks for the help , I have been stuck on this one for hours, what a waste of a day!


#11

Hello, just thought I’d jump back in here now that I understand what that odd-looking base code is. In this challenge:

Immediately Invoked Function Expression

we learn that the structure:

( function ( ) { doThisStuff; } ) ( );

creates a function that doesn’t need to be called. When the program comes across this IIFE, it will execute it immediately.

Pretty handy, I say, but it’s certainly confusing to see the syntax before we learn what it is.


#12

const sum1 = (function() {
“use strict”;
return function sum1(…args) {
//const args = [ x, y, z ];
return args.reduce((a, b) => a + b, 0);
};
})();
console.log(sum1(1, 2, 3)); // 6

Rather than rewriting, just omit ‘args=[x,y,z]’, ‘rest operator eliminates the need to check the args array’