Test Objects for Properties

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

Challenge: Testing Objects for Properties

Link to the challenge:

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.

2 Likes

@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
}

There is a difference between:

return obj.checkProp;

and

return obj[checkProp];

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.

const femaleSingersFavoriteIceCream = {
  Madona: 'vanilla',
  'Lady Gaga': 'vanilla',
};

console.log(femaleSingersFavoriteIceCream.Madona);
// vanilla

console.log(femaleSingersFavoriteIceCream.['Lady Gaga']);
// vanilla

In the second one we can’t use dot notation because “Lady Gaga” is not a valid JS identifier because it has a space in it.

If you remember, valid JS identifiers can be made of letters, numbers, underscore, and/or dollar sign - as long as it doesn’t start with a number.

2 Likes

Thanks for the clarification. I’ll bear the difference in mind going forward.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.