Set Default Parameters for Your Functions

So, I managed to clear the challenge using this code:


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

However, what is wrong with this code, which looks much simpler and cleaner:

const increment = (num,val=1)=>(num+val);
console.log(increment(5, 2)); // returns 7
console.log(increment(5)); // returns NaN

This code fails the test for some reason. Is it because I changed the syntax and the online judge couldn’t recognize what was happening or is something really wrong with my code?

My Browser Information:

User Agent is: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:60.0) Gecko/20100101 Firefox/60.0.

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

Both code worked for me. You simpler solution also worked.

And yes, you are right, the unit tests and the way FCC look for solution could be so syntax strict. However if you leave the original function(first code) as comment , it passes. Seems unit test looks for line, rather than statements.

This is not critical, as you could make it more simple! Awesome, now just grab a coffee and continue working on great work. Take life and code easy :smiley:

Keep going on great work, happy programming.

4 Likes

Thank you very much, @NULL_dev! :smiley:

Hello!
I have a doubt, Why in the code of FCC for this challenge a function returns other function how make the increment?
Is a tecnique or Doens’t matter?

pd: If i have some errors in my Engkish sorry, It ins’t my language!

Some FCC tests are based on syntax user input. but in real world, having such a test is not very logical and redundant.

In enterprise, and real development environment, this is the output is important, not the way you do it. Someone may go for a arrow function, while someone else as normal function, no matter as far as it works.

FCC is doing right thing, you can write a normal function to add two number, but the challenge is about to make sure you got some syntax of something.

Now about your question

Please note increment is a function pointer. A function pointer could point out to an existing function, or just has inline-block just like in the challenge sample.

This is not about returning another function.

2 Likes