Avoid Mutations and Side Effects Using Functional Programming

Avoid Mutations and Side Effects Using Functional Programming
0.0 0

#1

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


#2

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.


#3

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


#4

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


#5

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.


#6

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


#7

You should overwrite your code this way :

let x = fixedValue; 
return x + 1;

Then. Everything will be okay.