Challenge: Testing Objects for Properties

Somebody can help me?
on console.log(checkObj(“aleatorio”)); return “Not Found”, but there is a error cause the lesson don’t accept

const myObj = {
    gift: "pony",
    pet: "kitten",
    bed: "sleigh",
    city: "Seattle"

};

function checkObj(obj, checkProp, not) {

    if (myObj.hasOwnProperty(checkProp)) {
    return myObj[checkProp];}

    else if (myObj.hasOwnProperty(obj)) {
    return myObj[obj];} 

    else { (myObj.hasOwnProperty(not)) 
    myObj[not];
return "Not Found";}
};

console.log(checkObj("gift"));
console.log(checkObj("pet"));
console.log(checkObj("city"));
console.log(checkObj("bed"));
console.log(checkObj("aleatorio"));
**Your browser information:**

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

Challenge: Testing Objects for Properties

Link to the challenge:

I’m not sure where you got this, but adding this global variable is not part of the instructions.

You need to use this function argument.

You should not add this additional function argument.

You should not reference any global objects inside of your function.

Thx.

I see on youtube.

Now I did like this :

function checkObj(obj, checkProp) {

    if (checkObj.hasOwnProperty(checkProp)) {
    return checkObj[checkProp];}

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

console.log(checkObj("gift"));

But I don’t know where I put the
gift: “pony”,
pet: “kitten”,
bed: “sleigh”,
city: “Seattle”

Ahh… there are some very wrong, very old YouTube demos leading a lot of people to wrong answers on this problem.


This is very confusing formatting. Lets look at something clearer:

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

console.log(checkObj("gift"));

If you are seeing formatting like that on YouTube, I would totally ignore that YouTube content creator. That’s some weird stuff.


This line:

checkObj.hasOwnProperty(checkProp)

says "check if the function checkObj has the property checkProp. This isn’t what you want.

This line:

checkObj.hasOwnProperty(obj)

says "check if the function checkObj has the property obj. This is even less of what you want. the argument to hasOwnProperty needs to be a string, not an object.

You need to check if the object obj has the property checkProp.

2 Likes

Yeah! Thx a lot. I was completely wrong…

Now is working

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