Stuck on "Testing Objects for Properties"

Stuck on "Testing Objects for Properties"
0.0 0

#1

Can’t seem to pass this one. It tells me “myObj is not a function”. Why doe myObject have to be a function? Don’t get it.

TIA,

Gabriel

// Setup
var myObj = {
  gift: "pony",
  pet: "kitten",
  bed: "sleigh"
};

function checkObj(checkProp) {
  // Your Code Here
  if (myObj.hasOwnProperty(checkProp) === false) {
    return "Not found";
  } else if (myObj.hasOwnProperty(checkProp) === true)
  return myObj(checkProp);
}

// Test your code by modifying these values
checkObj("gift");

#2

That is because myObj is not a function. checkObj is a function as you have declared it as such. MyObj is simply an object with 3 property/value pairs.

You are 99% complete with this challenge. All you need to do is modify the following line in your code:

  return myObj(checkProp);

Think about what the function should be returning if myObj.hasOwnProperty(checkProp) === true.

UPDATE: A hint is you should not be using parentheses here.


#3

I already had:
return myObj(checkProp);

Seems like it should take it, not sure why it doesn’t.


#4

another hint: look back to the 3 “Accessing Objects Properties with…” exercises.


#5

You should not be using parentheses. When you make the statement myObj(checkProp), those parentheses tell javascript that myObj is a function with one parameter. Since myObj is an object and not a function, you get the error you are receiving.


#6

If you wanted to check properties of an object you should pass the object to the function. Anyway, functions are data types in Javascript so you can attach them to objects. This will do the trick


#7

// Setup
var myObj = {
gift: “pony”,
pet: “kitten”,
bed: “sleigh”,
checkObj : function(prop){
if(this.hasOwnProperty(prop)){
return this[prop];
} else {
return “Not found”;
}
}
};

alert(myObj.checkObj(“gifty”));


#8

This way function is attached to an object so it can be called directly on object to check if property exists. Sorry about indent though. There is also another way to solve this but I think this is the common practice and recommended one.


#9

And to correct code as is in your case would be

// Setup
var myObj = {
gift: “pony”,
pet: “kitten”,
bed: “sleigh”
};

function checkObj(checkProp) {
// Your Code Here
if (myObj.hasOwnProperty(checkProp) === false) {
	return "Not found";
} else if (myObj.hasOwnProperty(checkProp) === true){
	
// return myObj(checkProp); - here you return non-existing function call, myObj is an object not a function and checkObj(checkProp) is a proper function call
    return myObj[checkProp];
}

}

// Test your code by modifying these values
checkObj("gift");

#10

@mirkoacimovic Actually, the code you posted would not solve the challenge. Returning “Found” will not solve this challenge. The code is suppose to return the value of the property if found instead of the string “Found”.

@esclavosoy Make sure you capitalize Found in the following line. JavaScript is case-sensitive, so your logic would be correct, but would still fail the test because of the lower case “found”.

return "Not found";

#11

It took me a while but I finally got it. This is what finally took:

// Setup
var myObj = {
  gift: "pony",
  pet: "kitten",
  bed: "sleigh"
};

function checkObj(checkProp) {
  // Your Code Here
  if (myObj.hasOwnProperty(checkProp) === false) {
    return "Not Found";
  } else if (myObj.hasOwnProperty(checkProp) === true)
  return myObj[checkProp];
}

// Test your code by modifying these values
checkObj("gift");