Object.freeze not needed

Tell us what’s happening:
In this challenge you are going to use Object.freeze to prevent mathematical constants from changing. However, adding Object.freeze results in [TypeError: “PI” is read-only]. The current working solution is simply click “Run the Tests” with zero edits.

  **Your code so far**

function freezeObj() {
const MATH_CONSTANTS = {
  PI: 3.14
};
// Only change code below this line
Object.freeze(MATH_CONSTANTS);
// Only change code above this line
try {
  MATH_CONSTANTS.PI = 99;
} catch(ex) {
  console.log(ex);
}
return MATH_CONSTANTS.PI;
}
const PI = freezeObj();
  **Your browser information:**

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:95.0) Gecko/20100101 Firefox/95.0

Challenge: Prevent Object Mutation

Link to the challenge:

If you do this, you will not pass all tests for the challenge. You need to add the freeze.

Here, this says to try to change MATH_CONSTANTS and print out the error message if changing the value of PI was successfully prevented. Seeing a message that PI was not changed is a good thing.

The code I posted shows the use of Object.freeze

Right, but the original code does not have the freeze, so you can’t just

I think I get it. Because you put it in a try/catch, it looks like I’m getting an error. If this is true, this must cause a lot of confusion since the output changes immediately.

Yeah, the error message is confusing. I’m not sure why we didn’t change it to a ‘failed successfully’ message.

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