Tell us what’s happening:
I solved the challenge but I don’t understand why the function is written this way and how to read it. In particular, I’m very troubled by the use of the ‘increment’ function twice.
My best guess is:
- we declare a const increment that is a function
- this const function returns an increment function (different from the first increment function as it’s within the first function)
- the inner function takes 2 arguments, number and value and returns the sum of both
Is this interpretation correct?
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 6
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36
.
Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/es6/set-default-parameters-for-your-functions