ES6: Prevent Object Mutation error

I get this:

// running tests
PI equals 3.14.
// tests completed

when adding this:

Object.freeze(PI);

To my answer for this question. The validation doesn’t remove the last test ** PI equals 3.14 .** and allow submission to next question. Can you tell me why please?

Take a close look at the instruction again.

You need to freeze the MATH_CONSTANTS object so that no one is able alter the value of PI

You are essentially trying to freeze a property in the object not the object itself.

By changing Obj.freeze to MATH_CONSTANTS I get "TypeError: Cannot assign to read only property ‘PI’ of object ‘#<Object>’ "

Can you share your full code?

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

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

Make sure you spell out Object.

In this challenge you are going to use Object.freeze to prevent mathematical constants from changing.

Here is the code run with Object.freeze(PI):

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

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

Error is PI equals 3.14

Here is the code run with Object.freeze(MATH_CONSTANTS)

  "use strict";
  const MATH_CONSTANTS = {
    PI: 3.14
  };
  // change code below this line

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

Error is TypeError: Cannot assign to read only property ‘PI’ of object ‘#<Object>’
Here are the test params:
org
Thx for the help.

You removed the first line which declares the function in your 2nd code snippet.

that was a copy/paste error on my part. the code ran with everything intact.

Your code passes for me below.

Summary
function freezeObj() {
  "use strict";
  const MATH_CONSTANTS = {
    PI: 3.14
  };
  // change code below this line

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

Make sure you are using Chrome as your browser.

Hi,
Weird as this may sound, it validated (yes, in Chrome) even tho the console logged the obj error. I’m stumped (but happy). Thanks for the help.

It worked as our code went into exception block which means we successfully prevented from changing the obj. To conclude, test is expecting the error :wink: