Challenge: Catch the Error

This challenge will test your skills in implementing the strict mode in JavaScript.

Problem statement #

In this challenge, you are given an object, person. The function, func, is doing the following:

  1. modifying the name property of the person

  2. adding the age property to the person object

  3. deleting the person.name property

Your task is to modify the code so that none of the above changes are allowed. The code should throw an error if any of the changes mentioned above are made. For each error you catch you need to display the corresponding message on the console:

  1. "Cannot change name"

  2. "Cannot add property age"

  3. "Cannot delete person"

Input #

Performing an operation that is not allowed

Output #

The message corresponding to the error is displayed on the console

Sample input #

person.name = "xyz"
person.age = 30
delete person.name

Sample output #

"Cannot change name"
"Cannot add property age"
"Cannot delete person"

Level up your interview prep. Join Educative to access 80+ hands-on prep courses.