2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								---
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								id: 598f48a36c8c40764b4e52b3
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								title: Prevent Object Mutation
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								challengeType: 1
							 
						 
					
						
							
								
									
										
										
										
											2019-08-05 09:17:33 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								forumTopicId: 301207
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								---
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								## Description
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< section  id = 'description' >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								As seen in the previous challenge, < code > const< / code >  declaration alone doesn't really protect your data from mutation. To ensure your data doesn't change, JavaScript provides a function < code > Object.freeze< / code >  to prevent data mutation.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Once the object is frozen, you can no longer add, update, or delete properties from it. Any attempt at changing the object will be rejected without an error.
							 
						 
					
						
							
								
									
										
										
										
											2019-05-17 06:20:30 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								let obj = {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  name:"FreeCodeCamp",
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  review:"Awesome"
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								};
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Object.freeze(obj);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								obj.review = "bad"; // will be ignored. Mutation not allowed
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								obj.newProp = "Test"; // will be ignored. Mutation not allowed
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								console.log(obj); 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// { name: "FreeCodeCamp", review:"Awesome"}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								< / section >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								## Instructions
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< section  id = 'instructions' >  
						 
					
						
							
								
									
										
										
										
											2018-12-12 14:32:28 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								In this challenge you are going to use < code > Object.freeze< / code >  to prevent mathematical constants from changing. You need to freeze the < code > MATH_CONSTANTS< / code >  object so that no one is able to alter the value of < code > PI< / code > , add, or delete properties.
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								< / section >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								## Tests
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< section  id = 'tests' >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```yml
							 
						 
					
						
							
								
									
										
										
										
											2018-10-04 14:37:37 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								tests:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  -  text: Do not replace < code > const</ code >  keyword.
							 
						 
					
						
							
								
									
										
										
										
											2019-07-24 01:47:32 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    testString: getUserInput => assert(getUserInput('index').match(/const/g));
							 
						 
					
						
							
								
									
										
										
										
											2018-10-04 14:37:37 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  -  text: < code > MATH_CONSTANTS</ code >  should be a constant variable (by using < code > const</ code > ).
							 
						 
					
						
							
								
									
										
										
										
											2019-07-24 01:47:32 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    testString: getUserInput => assert(getUserInput('index').match(/const\s+MATH_CONSTANTS/g));
							 
						 
					
						
							
								
									
										
										
										
											2018-10-04 14:37:37 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  -  text: Do not change original < code > MATH_CONSTANTS</ code > .
							 
						 
					
						
							
								
									
										
										
										
											2019-07-24 01:47:32 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    testString: getUserInput => assert(getUserInput('index').match(/const\s+MATH_CONSTANTS\s+=\s+{\s+PI:\s+3.14\s+};/g));
							 
						 
					
						
							
								
									
										
										
										
											2018-10-04 14:37:37 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  -  text: < code > PI</ code >  equals < code > 3.14</ code > .
							 
						 
					
						
							
								
									
										
										
										
											2019-07-24 01:47:32 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    testString: assert(PI === 3.14);
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< / section >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								## Challenge Seed
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< section  id = 'challengeSeed' >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< div  id = 'js-seed' >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								function freezeObj() {
							 
						 
					
						
							
								
									
										
										
										
											2018-10-16 05:34:52 +05:30 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  'use strict';
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								  const MATH_CONSTANTS = {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    PI: 3.14
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  };
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  // change code below this line
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  // change code above this line
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  try {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    MATH_CONSTANTS.PI = 99;
							 
						 
					
						
							
								
									
										
										
										
											2019-03-29 09:36:58 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  } catch(ex) {
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								    console.log(ex);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  return MATH_CONSTANTS.PI;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								const PI = freezeObj();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< / div >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< / section >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								## Solution
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< section  id = 'solution' >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
									
										
										
										
											2018-10-16 05:34:52 +05:30 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								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;
							 
						 
					
						
							
								
									
										
										
										
											2019-03-29 09:36:58 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  } catch(ex) {
							 
						 
					
						
							
								
									
										
										
										
											2018-10-16 05:34:52 +05:30 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    console.log(ex);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  return MATH_CONSTANTS.PI;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								const PI = freezeObj();
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
									
										
										
										
											2019-07-18 08:24:12 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								< / section >