Use the Rest Operator with Function Parameters. Unsure of why after looking into it

It is probably a minor issue like being in the wrong place but I am mystified by this. SO … is spread and … is rest if put in a function argument space. Why is my code broken? Thank you.

Your code so far


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

Your browser information:

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

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

Rest operator will take x number of arguments and will spread (place) them into an array.

Therefore you can simply name your rest operator args to match the name of the array inside. Then you won’t need to separately assign parameters to args again like this.

I have placed the rest operator in every possible position. I’m shocked I haven’t gotten the answer by accident yet. Free Code Camps algorithm can be very specific, more so then actual javaScript. IDK

Can you share your updated code?

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

What are x, y and z? These are things you have added to the challenge that it never asked for.


This function takes one argument, that argument will be available as a variable called a inside the function

function example1 (a) {

This function takes two arguments, they will be available as the variables a and b inside the function

function example2 (a, b) {

function takes one or more arguments, they will be available as the variable c, which will be an array containing whatever was passed to the function.

function example3 (...c) {

freeCodeCamp pre-load x,y,z into the code for me. I only added …xx

Ah sorry about that, I didn’t check the challenge first. Point still stands:

look at those three examples, which one would apply if you wanted a function that took any number of numbers so you could add them up?

Are you/have you done this problem? Do you really think that isn’t the first thing I did?

Going by the code you originally pasted, no, nobody thought that was the first thing you did. Your problem lies on this line now:

const args = [x, y, z];

Which is both redeclaring args (not allowed), and it’s referencing an x, y, and z variables that don’t exist. Just removing that line ought to make things work.

freeCodeCamp pre-loaded that into the question. I didn’t write anything. Ill iterate again I have done every possible combo of answers. Did you do this question? If I do args = […x] the output will be y and z are undefined.

SOMEONE WHO HAS DONE THIS QUESTION PLEASE HELP.

It doesn’t matter what the pre-loaded code is, since you are expected to modify it. Anyway, args is already a function parameter, specifically it’s an array. That’s what the “rest operator” does, it gobbles up all remaining parameters and sticks them into an array. Therefore it doesn’t need to be re-declared, so you can just delete the destructuring line that had declared args previously.

PS: I’ve done the JS curriculum twice now.

So my last example. The function takes one or more arguments, which will be available as the variable x, which will be an array containing whatever was passed to the function.

function example3 (...x) {

It wants you to call them args, not x. Again, to reiterate, in this function, x will be an array containing whatever was passed to the function