Challenge: Catch the Error
This challenge will test your skills in implementing the strict mode in JavaScript.
We'll cover the following
Problem statement #
In this challenge, you are given an object, person
. The function, func
, is doing the following:
-
modifying the
name
property of theperson
-
adding the
age
property to theperson
object -
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:
-
"Cannot change name"
-
"Cannot add property age"
-
"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 70+ hands-on prep courses.