ES6 - Set Default Parameters for Your Functions

Hello! I have a problem with the challenge ES6 - Set Default Parameters for Your Functions.

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

const increment = (function() {
  "use strict";
  return function increment(number, value) {
    return number + value;
  };
})();
console.log(increment(5, 2)); // returns 7
console.log(increment(5)); // returns NaN


I did it like this and it doesn't work, what am I doing wrong (only the first line is changed)?

const increment = (function(number, value = 1) {
  "use strict";
  return function increment(number, value) {
    return number + value;
  };
})();
console.log(increment(5, 2)); // returns 7
console.log(increment(5)); // returns NaN
1 Like

share the challenge link.

You are adding the default parameter at wrong place.

Try this

const increment = (function() {
      “use strict”;
      return function increment(number, value=1) { 
             return number + value;
      };
})();
5 Likes

This worked, thank you!

Try this: It worked.

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

Sorry Guys I have a question dealing this quiz I did it as it supposed to be but keep returning a NaN output to me when I change the value to value = 1 it worked why? it shouldn’t be like that I guess??

If you go to the challenge and click on the ask for help button it will open a new topic that includes your code. Maybe someone will spot the error

This challenge has now been reworked, using arrow functions. This makes the code much smaller, and the answer to the challenge much easier. This is what I have:

<redacted>

please don’t share full working solutions just for the sake of sharing it

My apologies. I had seen two others with answers listed, so I thought it was perfectly acceptable to do. In the future, I’ll be sure to not participate with providing solutions, like other users.

1 Like