Tell us what’s happening:
I edited this code and read it several times but I can’t see what I am doing wrong.
I keep getting this TypeError: Cannot read property ‘message’ of undefined
**Your code so far**
function checkObj(obj, checkProp) {
// Only change code below this line
if (obj.hasOwnProperty(checkProp))
{
return obj[checkProp];
}
else if(obj.hasOwnProperty(checkProp) !== true)
{
return "Not Found";
}
return "Change Me!";
}
// Only change code above this line
**Your browser information:**
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36
When I cut and paste your code, aside from lacking standard formatting, it works for me.
I’m not sure what that error message means - it doesn’t have anything to do with your code. There might be some error happening behind the scenes.
Sometimes these things happen. Reboot, clear the browser cache, try a different browser… If none of that works, just back out and move onto the next challenge.
@kevinSmith , I kept failing this test using the dot notation but passed immediately I used the bracket notation. Please, why’s that? This is my code (using dot notation) which kept failing;
function checkObj(obj, checkProp) {
// Only change code below this line
if(obj.hasOwnProperty(checkProp)){
return obj.checkProp;
} else {
return "Not Found";
}
// Only change code above this line
}
whereas, I passed using the bracket notation as follows;
function checkObj(obj, checkProp) {
// Only change code below this line
if(obj.hasOwnProperty(checkProp)){
return obj[checkProp];
} else {
return "Not Found";
}
// Only change code above this line
}
The first one checks for a property named “checkProp”. The second one checks for a prop whose name matches the string contained in the variable named checkProp.
const obj = { foo: 'bar' };
const checkProp = 'foo';
console.log(obj.checkProp);
// undefined
console.log(obj.[checkProp]);
// bar
console.log(obj['foo']);
// bar
console.log(obj[foo]);
// Uncaught ReferenceError: foo is not defined
In the last example, it crashes because you are trying to access a variable named “foo” that doesn’t exist.
In general, you will use dot notation if you know the property at the time of writing the code.
The two cases where bracket notation would be used are if the name of the property is stored in a variable (like in the challenge here) or if the property is not a valid JS identifier.