It seems that the following code passes the test. But shouldn’t. I’m not sure where to report it. It’s a minor thing, but nevertheless.
Thanks for the great site.
Your code so far
// The global variable
var fixedValue = 4;
function incrementer () {
// Only change code below this line
return ++fixedValue;
// Only change code above this line
}
Challenge: Avoid Mutations and Side Effects Using Functional Programming
There is nothing wrong with this test.
You just use the value of the variable, not the variable itself.
I will give you example where you have side effects:
var fixedValue = 4;
function incrementer () {
// Only change code below this line
fixedValue += 1;
// Only change code above this line
}
console.log(fixedValue); // 5
Your function incrementer should not change the value of fixedValue (which is 4 ).
However ++fixedValue changes the value of the variable… Am I missing something? I understand that the value is changed upon function execution so I would have to do
// The global variable
var fixedValue = 4;
function incrementer () {
// Only change code below this line
return ++fixedValue;
// Only change code above this line
}
incrementer();
console.log(fixedValue); // gives 5 (AND PASSES THE TEST)
If that’s intended - that’s fine. I thought that the “correct” solution was something like this:
I did it yesterday and I’m agree with you. Both ++var and var++ changes the fixed var. The correct result must be 4 while the function should return 5 (or param+1). I did a copy and then ++.
Yes, I agree. If the point of the exercise is to demonstrate a pure function that doesn’t change the value of ‘fixedValue’, then any code that changes it’s value when ‘incrementer’ is called should fail the test, should it not? If one changes ‘fixedValue’ to a const, it might serve to demonstrate how the code will fail if it seeks to change the value of ‘fixedValue’.
Thank you for helping make FCC better. Bugs can be reported as GitHub Issues. Whenever reporting a bug, please check first that there isn’t already an issue for it and provide as much detail as possible.