I need help with this challenge: Prevent Object Mutation

Tell us what’s happening:
Describe your issue in detail here.
I need help with this challenge,
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();
( You should not replace the const keyword.

Passed

MATH_CONSTANTS should be a constant variable (by using const ).

You should not change the original declaration of MATH_CONSTANTS .

PI should equal 3.14 .)
It is |ES6 Prevent Object Mutation, I’ve tried the given solution couldn’t go through, please it takes more than an hour!
thanks InAdvace !
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 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36

Challenge: Prevent Object Mutation

Link to the challenge:

with Object.freeze you don’t send it a string with the name of the object you send it the reference to the object, the variable name.

For example:

const myObj = { num: 123 };

Object.freeze("myObj"); // won't work, don't send the string

Object.freeze(myObj);   // will work, sending the reference
1 Like

There is also an issue with this line:

  PI:3.14

You changed it by removing the space after the colon. I think you have to replace it to get it to pass.

1 Like

Thank you very much for your help, yes this is was the point and also I fixed the last line according to the solution requirement, PI should equal 3.14
it was : const PI = freezeObj();
replaced to : const PI = 3.14;

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