freeCodeCamp/guide/chinese/certifications/javascript-algorithms-and-d.../functional-programming/avoid-mutations-and-side-ef.../index.md

821 B

title localeTitle
Avoid Mutations and Side Effects Using Functional Programming 使用功能编程避免突变和副作用

使用功能编程避免突变和副作用

问题解释

填写函数incrementer的代码,使其返回全局变量fixedValue的值增加1。 fixedValue不应该改变,不管功能多少次incrememter被调用。

提示1

fixedValue上使用增量运算符( ++ )将改变fixedValue ,这意味着它将不再引用它所分配的相同值。

解:

// the global variable 
 var fixedValue = 4; 
 
 function incrementer () { 
  // Add your code below this line 
  return fixedValue + 1; 
 
  // Add your code above this line 
 } 
 
 var newValue = incrementer(); // Should equal 5 
 console.log(fixedValue); // Should print 4