Problem with "Set Default Parameters for Your Functions" (JS ES6 syntax)

Problem with "Set Default Parameters for Your Functions" (JS ES6 syntax)
0

#1

Tell us what’s happening:

Hi,

I followed the ES6 instructions to no avail…I really don’t understand what’s wront with my code and I searched for an answer.

Your code so far


const increment = (function() {
  "use strict";
  return function increment(number,value+=1) {
    

    
    return number + value;
    
  };
})();
console.log(increment(5, 2)); // returns 7
console.log(increment(5)); // returns NaN

Your browser information:

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

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/es6/set-default-parameters-for-your-functions


#2

ok, let’s see if this helps.
The instructions provided are:

Modify the function increment by adding default parameters so that it will add 1 to number if value is not specified.

and the lines of code we are interested in therefore are these:

  return function increment(number, value) {
    return number + value;
  };

Right now if value is not specified, the function will fail, correct? (it expects someone to pass a number and a value and doesn’t handle it if value is not provided).

The exercise is teaching us a way to allow the function to handle it if someone only gives us ‘number’ and doesn’t give us ‘value’ when they call increment.

The method being taught is to set a ‘default value’ for a given parameter.
For eg. they gave us this:

function greeting(name = "Anonymous") {
  return "Hello " + name;
}

If you pay attention to the ‘name’ parameter, there is an equal sign and a value of “Anonymous”
This part is saying, if ‘name’ is not given to us, assume that name is “Anonymous”

Does this seem clear?
Now when the function is called it will work even if i neglect the parameter like this:

greeting();

Then it will output ‘Hello Anonymous’
And it will also continue to work if I give it the name like this

greeting('hbar1st');

In the exercise they want you to change the ‘value’ parameter so it has a default.
Specifically

Modify the function increment by adding default parameters so that it will add 1 to number if value is not specified.

So if I call increment like this:

increment(10,5);

It currently adds 10 with 5 (number is added to value)

But they want it to work differently if I neglect to provide value

increment(10);

In this case it should return 11.

So using the method they showed us (with the equal sign added to the parameter declaration)
do you think you can solve this now?

happy to help further if the above is not enough.


#3

Yes I understood that. But this is what I wrote in “return function increment(number,value+=1)”

No?


#4

the += is not valid in this case.
You can use += in the body of the function but not in the declaration.

If you look at the sample code again you will see they have just a single ‘=’ sign and the default value.
Try to use the same way to solve this.


#5

I got it! It was just “value =1”, so simple! I got mixed up wit the instruction .

But if value = value +1, when value is 0=, then default value will always be 1! But result is “NaN”, meanin g “not defined”, so its not 0

My assumtiopn was that a non determined parameter sets to 0, which is wrong.


#6

awesome! Glad you figured it out. Easy right!