Code is NOT working, even the Hint: Solution

Tell us what’s happening:

I used Object.freeze() on MATH_CONSTANTS to freeze the variable so it is no longer mutable. However, I am getting the error:

[TypeError: Cannot assign to read only property ‘PI’ of object ‘#’]

This error is happening even when I copy the exact solution in GET A HINT which is exactly what I wrote in the first place.

What is happening here?

Thanks!

You should not change original `MATH_CONSTANTS` .

`PI` should equal `3.14` .

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

function freezeObj() {

"use strict";

let MATH_CONSTANTS = {

PI: 3.14

};

Object.freeze(MATH_CONSTANTS);

try {

MATH_CONSTANTS.PI = 99;

} catch (ex) {

console.log(ex);

}

return MATH_CONSTANTS.PI;

}

const PI = freezeObj();

[TypeError: Cannot assign to read only property 'PI' of object '#<Object>']

You should not change original `MATH_CONSTANTS` .

`PI` should equal `3.14` .

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

function freezeObj() {

"use strict";

let MATH_CONSTANTS = {

PI: 3.14

};

Object.freeze(MATH_CONSTANTS);

try {

MATH_CONSTANTS.PI = 99;

} catch (ex) {

console.log(ex);

}

return MATH_CONSTANTS.PI;

}

const PI = freezeObj();

[TypeError: Cannot assign to read only property 'PI' of object '#<Object>']

 Object.freeze(MATH_CONSTANTS);

Your code so far


function freezeObj() {
"use strict";
let MATH_CONSTANTS = {
  PI: 3.14
};

Object.freeze(MATH_CONSTANTS);

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 11_1_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36.

Challenge: Prevent Object Mutation

Link to the challenge:

The starter code says

function freezeObj() {
  const MATH_CONSTANTS = {
    PI: 3.14
  };
  // Only change code below this line


  // Only change code above this line
  try {
    MATH_CONSTANTS.PI = 99;
  } catch(ex) {
    console.log(ex);
  }
  return MATH_CONSTANTS.PI;
}
const PI = freezeObj();

It looks like you changed code outside of the area where you should have.

For some reason, the solution also contained that modification, so I fixed it.

1 Like

omg thank you, the error was happening BECAUSE of the attempt to change … I just assumed there was an error and never submitted, but it was right the whole time LOLOLOL sorry thanks!