Avoid Mutations and Side Effects Using Functional Programming

Tell us what’s happening:

Your code so far


// the global variable
var fixedValue = 4;

function incrementer () {
  // Add your code below this line
 

   return  fixedValue;
   fixedValue ++;
  // Add your code above this line
}

var newValue = incrementer(); // Should equal 5
console.log(fixedValue); // Should print 4

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/functional-programming/avoid-mutations-and-side-effects-using-functional-programming

When the function is called, the return fixedValue just returns the value 4.

If you put the fixedValue ++ on the line before the return statement, then you would return the value of 5, but you would be changing the value of the global value fixedValue which you are not supposed to do based on the challenge instructions.

so,I don’t know how to figure it out ,only one can get a check

Did you change your code? If so, post it again, so we can take a look at what you tried differently this time.

The requirements are asking you to return the value of fixedValue incremented by one, but not change it

One of the core principle of functional programming is to not change things

So you must return 5 from the function, while still leaving fixedValue at 4.

2 Likes

I get it, this is my result:let x=fixedValue+1; return x;

You should overwrite your code this way :

let x = fixedValue; 
return x + 1;

Then. Everything will be okay.

6 Likes

i just wrote :

return fixedValue +1;
and it worked perfectly

1 Like

// the global variable
var fixedValue = 4;

function incrementer () {
// Add your code below this line

return (fixedValue,5);
// Add your code above this line
}

var newValue = incrementer(); // Should equal 5
console.log(fixedValue); // Should print 4

I am having problems with this lesson. I put the correct code but, the website said it is not correct.

Please help me!

Cristian Prieto

If you have a question about a specific challenge as it relates to your written code for that challenge, just click the Ask for Help button located on the challenge. It will create a new topic with all code you have written and include a link to the challenge also. You will still be able to ask any questions in the post before submitting it to the forum.

Thank you.