Incrementer function

Hello,
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/functional-programming/avoid-mutations-and-side-effects-using-functional-programming
Why this doesn’t work, with ++ ?

// The global variable
var fixedValue = 4;

function incrementer () {
  // Only change code below this line
return fixedValue++;

  // Only change code above this line
}
let newValue = incrementer();
console.log(newValue)

I understood:
fixedValue++ change the fixedValue.
fixedValue + 1 is just a computation. That don’t change fixedValue.

This happens because you’re using the post-increment operator (value++), so the increment only occurs after the return statement has run. You can use the pre-increment operator (++value) to increment the value before the return statement runs.

Here’s an example:

function postIncrementor() {
    return originalValue++;
}

function preIncrementor() {
    return ++originalValue;
}

var originalValue = 0;
var newValue = null;

console.log("\n*********** Values in default state **************\n");

console.log(`Original value: ${originalValue}`);
console.log(`New value: ${newValue}`);

newValue = postIncrementor();

console.log("\n*********** Values after postIncrementor() **************\n");

console.log(`Original value: ${originalValue}`);
console.log(`New value: ${newValue}`);

originalValue = 0;
newValue = null;

console.log("\n*********** Reset values to default state **************\n");

console.log(`Original value: ${originalValue}`);
console.log(`New value: ${newValue}`);

newValue = preIncrementor();

console.log("\n*********** Values after preIncrementor() **************\n");

console.log(`Original value: ${originalValue}`);
console.log(`New value: ${newValue}`);

Here’s the output:

*********** Values in default state **************

Original value: 0
New value: null

*********** Values after postIncrementor() **************

Original value: 1
New value: 0

*********** Reset values to default state **************

Original value: 0
New value: null

*********** Values after preIncrementor() **************

Original value: 1
New value: 1

This has nothing to do with pre vs post.

How so? The code sample was returning the value before the increment, and that was the cause of the OP’s confusion, correct? They were wondering why the newValue wasn’t reflecting the increment, and that was because they used the incorrect increment operator.

It may not be valid for the challenge they are attempting, but what I pointed out seems valid for the confusion they are facing in their code sample, correct?

They gave the wrong link. The challenge is about making the function return one more than the current value of the global variable without changing the global variable. Your long post about pre vs post is unrelated to their core problem with doing that.

Right, but they seem to be confused also by the fact that the value returned wasn’t incremented, regardless of the challenge. That was the issue I was referring to. I’ll check out the challenge as well and post something related to that.

yes I made a mistacke with the link. I will correct it.

You are correct in your understanding!

You shouldn’t modify the fixedValue in your example. You should only be returning whatever fixedValue is incremented by 1.

So, after the function runs, fixedValue should remain unchanged! The pre/post-increment operators will change the value of fixedValue, so you shouldn’t use those for this particular challenge.

It seems like you understand the core problem of that challenge just fine here. The big idea in this lesson and the one you linked is to be careful changing global variables.


If your code needs you to think carefully about pre vs post incrementing, then your design is brittle and a maintainability liability. Pick one (postfix incrementing is more common) and use it consistently.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.