Use the Rest Operator with Function Parameters

Use the Rest Operator with Function Parameters
0

#1

Tell us what’s happening:

Your code so far


const sum = (function() {
  "use strict";
  return function sum(...args) {
    const sum = [];
    sum[0] = 0;
    for(let i = 0; i < args.length; i++){
      sum[0] += args[i]; 
    }
    return sum[0];
  };
})();
console.log(sum(1, 2, 3)); // 6

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; 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

You named the variable the same of the function called: change that and it should work :slight_smile:


#3

Hi @Layer, Can you please explain how @Dfs1234 came up with this solution?

Thank you !


#4

I can explain what his code does^^
I understand it could be a bit weird but hey, it works! :stuck_out_tongue:
This is what you’re being asked to write ( the solution presented in this topic):

  return function sum(...args) {
    const sum = [];
    sum[0] = 0;
    for(let i = 0; i < args.length; i++){
      sum[0] += args[i]; 
    }
    return sum[0];
  };

Now, here was the first error:

    const sum = [];
    sum[0] = 0;

sum already exists and it’s a function, so you cannot declare it again as an array. This is what prevented the code to be executed correctly, but there’s another weird thing: the sum array is declared as an empty array and the next row it assigns as first element the 0 value, which is the element the function keep working on and return at the end.
It would be the same to declare a new variable like let newVar = 0 and use it as main variable: the same code could have been written:

  return function sum(...args) {
    let newVar = 0
    for(let i = 0; i < args.length; i++){
       newVar  += args[i]; 
    }
    return  newVar;
  };

After the variable declaration it loops through the args array - as many times as the arguments array length - incrementing each time the value of its main variable by the value of the argument.

for(let i = 0; i < args.length; i++){
       newVar  += args[i]; 
    }

it means that if the function would be called with these arguments: sum(5, 10, 15) the loop will behave as follows:

First round:
newVar = 0;
i = 0;
newVar = newVar + args[0] --> args[0] has value 5 so —>newVar = 0 + 5

Second round:
newVar = 5;
i = 1;
newVar = newVar + args[1] --> args[1] has value 10 so —> newVar = 5 + 10

Third round:
newVar = 15;
i = 2;
newVar = newVar + args[2] --> args[2] has value 15 so —> newVar = 15 + 15

At the end it returns newVar and that’s all ^^

About the challenge: there could be a few things not superclean ( i don’t remember the previous challenges):

  • the pattern used there is called IIFE ( you can find some reference here: MDN - IIFE ); nothing related to the challenge itself, it’s just used for testing purpose I guess^^

  • The rest operator, the actual way to access arguments ( MDN - Rest parameters ) and the subject of the challenge - basically it takes every argument not explicitly named and put it into an array.

I’m not sure if your doubts are being caught in this answer: if there’s still something not clear ask again! :slight_smile:


#5

Thanks man.Your soul so brightly.