Prevent Object Mutation

Prevent Object Mutation
0.0 0

#1

what is the mistake i have done in this coding ?? its not working help me

Your code so far


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

  // 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/66.0.3359.181 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/es6/prevent-object-mutation


#2

HI @arrbxr

You may want to read the challenge description again, specifically the bit about using Object.freeze


#3

thank you so much :smile:


#4

Hi,

I have this solution but not working.

 // change code below this line

  Object.freeze(MATH_CONSTANTS.PI);

  // change code above this line

What am I doing wrong?

Thanks


#5

OK I have got the right result now.
First I thought I have to use the method on the Object's property but then I realised I have to use it on the whole Object to freeze its properties.


#6

Its a object, .PI accesses the 3.14 not the whole object itself

MATH_CONSTANTS is the whole object you want to freeze

MATH_CONSTANTS.PI is how you access the 3.14 inside (belongs to PI)


#7

This is my right result:

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();