Testing Objects for Properties Lookup

Tell us what’s happening:

function checkObj(obj, checkProp) {
  // Only change code below this line
 var obj = {
   gift: "pony",
   pet: "kitten",
   bed: "sleigh",
   city: "Seattle",};
 return obj[checkProp];

console.log(checkObj.gift);

result is undefined
i thought this code should return “pony”
i don’t understand :blush:

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

1 Like

Why did you redefine obj?

Your code is incomplete, so I’m not sure exactly what you are seeing.

I brought it to the console to check i is gift associated with and pony

It should not be inside of your function. Also, where is the rest of your function?

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

something like that
still doesn’t work

Please copy-paste absolutely every piece of code that you have. That is still an incomplete chuck of code and I can’t help with incomplete code.

function checkObj(obj, checkProp) {

  // Only change code below this line

 var obj = {

   gift: "pony",

   pet: "kitten",

   bed: "sleigh",

   city: "Seattle",};

 return obj[checkProp];

if (checkObj.hasOwnProperty(obj, checkProp)) return [checkProp];

else if (checkObj.hasOwnProperty(obj)) return [checkProp];

else return "Not Found";

       

  // Only change code above this line

}

console.log(gift);
function checkObj(obj, checkProp) {
  // Only change code below this line
  /*
  As I said, this **must** be removed
   var obj = {
     gift: "pony",
     pet: "kitten",
     bed: "sleigh",
     city: "Seattle",
  };
  */

 return obj[checkProp];

  // Nothing after this point will run because your function has 'return'ed

  // checkObj doesn't exist!!
  if (checkObj.hasOwnProperty(obj, checkProp))
    return [checkProp]; // why return an array?
  else if (checkObj.hasOwnProperty(obj))
    return [checkProp]; // why return an array?
  else
    return "Not Found";

  // Only change code above this line
}

console.log(gift); // 'gift' is not a defined variable, so this will log 'undefined' to the console

I don’t understand, if don’t indicate the check field (gift pony etc),
how does the system understand what to compare with what
so why should you delete ** var field**?

The object are property defined outside of your function, not inside.

function printThis(thisThing) {
  console.log(thisThing); // thisThing is passed in
}

// Inputs defined out here
let thingToPrint = "Test, test, test";
printThis(thingToPrint);
1 Like

Hi @good.answer.e !

You want to be able to write a function that works with ANY object.
Not just one you hardcoded yourself.

When you call the function that is where you are going to use the object and check if the value exists or not.

Here are some function calls

checkObj({food: "pizza", dessert: "cake", drink: "coke"},"food")
checkObj({car: "ford", color: "black", model: "fusion"},"miles")

I just created two random objects.

Am I going to hardcode that into the function?
No.

What if I want to test my function with 100 different objects?
Am I going to hardcode all of those key value pairs?

No.

When you call the function then you pass through the object you want to check if the property exists or not.

The computer is going to only look at that first argument in the function call and check to see if the property exists or not.
It will not have a database and just know if it exists or not.

Hope that makes more sense!

1 Like

i got it finally!! and did it
two days trying to create an arrey that will work inside the function
did’t undestood that tests use allready some arrey) :laughing:

function checkObj(obj, checkProp) {

  // Only change code below this line

  return obj[checkProp] ?? "Not Found";

  // Only change code above this line

}

Hi @linhneymar369 !

Welcome to the forum!

This post hasn’t been active for over 6 months.
If you have a question about a challenge then please create your own post.

Also, please don’t just give out answers.
This a teaching forum.
It is better to guide the user to the correct answer through hints instead of posting full solutions.

Thanks!