const
declaration alone doesn't really protect your data from mutation. To ensure your data doesn't change, JavaScript provides a function Object.freeze
to prevent data mutation.
Once the object is frozen, you can no longer add, update, or delete properties from it. Any attempt at changing the object will be rejected without an error.
let obj = {
name:"FreeCodeCamp",
review:"Awesome"
};
Object.freeze(obj);
obj.review = "bad"; // will be ignored. Mutation not allowed
obj.newProp = "Test"; // will be ignored. Mutation not allowed
console.log(obj);
// { name: "FreeCodeCamp", review:"Awesome"}
Object.freeze
to prevent mathematical constants from changing. You need to freeze the MATH_CONSTANTS
object so that no one is able to alter the value of PI
, add, or delete properties.
const
keyword.
testString: getUserInput => assert(getUserInput('index').match(/const/g), 'Do not replace const
keyword.');
- text: MATH_CONSTANTS
should be a constant variable (by using const
).
testString: getUserInput => assert(getUserInput('index').match(/const\s+MATH_CONSTANTS/g), 'MATH_CONSTANTS
should be a constant variable (by using const
).');
- text: Do not change original MATH_CONSTANTS
.
testString: getUserInput => assert(getUserInput('index').match(/const\s+MATH_CONSTANTS\s+=\s+{\s+PI:\s+3.14\s+};/g), 'Do not change original MATH_CONSTANTS
.');
- text: PI
equals 3.14
.
testString: assert(PI === 3.14, 'PI
equals 3.14
.');
```