I am solving this exercise, I have a constant but I want the PI property of the object to be immutable, I want to freeze it, I use Object.freezer (MATH_CONSTANT

Tell us what’s happening:
Describe your issue in detail here.

   **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 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36

Challenge: Prevent Object Mutation

Link to the challenge:

Hello there.

Do you have a question?

If so, please edit your post to include it in the Tell us what’s happening section.

Learning to describe problems is an important part of learning how to code.

Also, the more information you give us, the more likely we are to be able to help.


What trouble are you having?

I am solving this exercise, I have a constant but I want the PI property of the object to be immutable, I want to freeze it, I use Object.freezer (MATH_CONSTANTS) and it gives me error. I don’t understand why, so it should happen to Object.freezer (PI).

Your code is fine. It is supposed to throw the [TypeError: "PI" is read-only] if that is what you are worried about.

Click Run the Test button and see what happens

2 Likes

What do you mean by error? It should try to change the object but fail to do so. What happens when you run the tests?

1 Like

I need to freeze the MATH_CONSTANTS object so that no one is able to alter the value of PI , add, or delete properties. But say this by console [TypeError: Cannot assign to read only property ‘PI’ of object ‘#’]

This message means that your object can’t be changed. So it sounds like you prevented the object from being changed.

1 Like