Https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/es6/set-default-parameters-for-your-functions/

Https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/es6/set-default-parameters-for-your-functions/
0.0 0

#1

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

My code runs on the console at jsbin and replit but it doesnt run on the fcc editor. HELP please

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


console.log(increment(5, 2)); // returns 7
console.log(increment(5)); // returns NaN```

#2

I just did that one, the instructions aren’t very clear - what they are looking for is this…

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

This will set value to 1 if no argument is given there


#3

It runs, but not because it should. Just because…javascript

Notice the ( before the keyword function? This means that this function is an IIFE (immediately invoked function expression).

It will automatically execute when it’s called, and it will assign it’s return value to the variable increment. And it’s return value is the next function.

So why shouldn’t it work?

// You are trying to assign a default value to
// the first number parameter
// but this runs immediately and never uses that variable
const increment = (function(number =+ 1) {
  "use strict";

// you only have access to these parameters
// therefore only these can be set to defaults
  return function increment(number, value) {
    return number + value;
  };
})();

From now if you see an IIFE, just keep in mind you don’t have access to those parameters (there is a way, but rarely used, so no need to complicate it)

So it runs in the repl because those values are never used. It fails FCC tests because they’re checking for another parameter to have a default value.


#4

Thanks alot guys, I was actually losing it when my code wouldn’t run😂. Cheers


#5
const increment = (number, value = 1) => number + value;
console.log(increment(5, 2)); // returns 7
console.log(increment(5)); // returns NaN