Is the exercise wrong?

Tell us what’s happening:
I have done this exercise as it is explained on the video, which to me makes total sense. However I am not passing this test and when I reset the code it gives me this:

function checkObj(obj, checkProp) {

// Only change code below this line

return “Change Me!”;

// Only change code above this line

A function where the user is gonna input the object and ask to check if it has a certain property. Why would it need to ask for checking anything, if the user is inputting all the properties? It makes no sense to me. My code below is similar to the one on the video but the code is not passing the test
Your code so far


var obj = {
"gift": "pony",
"pet": "kitten",
"bed": "sleigh",
"city": "Seattle"
}

function checkObj(checkProp) {
var checkProp;
if (obj.hasOwnProperty(checkProp))
{
  return obj[checkProp]
}
else {
  return "Not Found"
}
}

console.log(checkObj("city"))

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36.

Challenge: Testing Objects for Properties

Link to the challenge:

Hi. It isn’t made explicit in the challenge introduction, but function checkObj should have two arguments, a first one for the object and a second one for the property to check. Even though you are checking if a property exists for the object obj, which is declared globally, you may want to use the same function for a different object.

It’s made explicit in the sample code:

function checkObj(obj, checkProp) {
  // Only change code below this line
  return "Change Me!";
  // Only change code above this line
}

This is why copying the videos is bad.

In OP’s code, obj isn’t a function’s argument, it’s being accessed from the function, that’s what i meant.

1 Like

Videos have helped me a lot. I like how the guy explains it all, sometimes the written instructions are not clear enough.

I got confused because the video and the instructions on this one were greatly different.

Anyway, I skipped this one and then came back to it with its solution, thanks people.

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
}
console.log(checkObj({gift: "pony", pet: "kitten", bed: "sleigh"}, "gift"))

The videos are helpful, but I always caution against relying on them too much because

  1. The videos run out at some point and you need to be ready to code without them.
  2. The videos don’t always have the most up to date changes to the curriculum.

That said, I’m glad you figured it out!

Side note - your brace style is atypical

One True Brace Style

if (foo) {
  bar();
} else {
  baz();
}

Stroustrup

if (foo) {
  bar();
}
else {
  baz();
}

Allman

if (foo)
{
  bar();
}
else
{
  baz();
}

Are the most common style choices.