Testing Objects for Properties last request

Regarding this topic in Basic JS :
I pass all step meanwhile the last one, the last one ask this topics :
checkObj({pet: "kitten", bed: "sleigh"}, "gift") should return the string Not Found .

I don’t understand what the question ask it a bit confusing for me, any idea please ?

Please find below my code :

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

function checkObj(obj, checkProp) {
  // Setup
  var test = myObj.hasOwnProperty(checkProp);
  if (test == true) {
    return myObj[checkProp];
  }
  else {
      return "Not Found"
  }

  // Only change code below this line
  return "Change Me!";
  // Only change code above this line
}

the function is being passed an object and a string, different each time

in this case the object is {pet: "kitten", bed: "sleigh"}, and the string is gift, so your code should check if the object has a property named gift, as it doesn’t have it the function should return Not found

1 Like

Hi Ilenia,
Thanks for your proposal, you tell me the object is {pet: “kitten”, bed: “sleigh”} and the string is gift. The problem is my entire object is dedicated by the name : myObj
Do you have any idea to share me more than your definition please ?
Many thanks

The function has two parameters, these take the values of what is passed in

The tests you are not passing are there to make sure you must use those and don’t hardcode anything

So in that case obj is

And checkProp is

If you have issues with this I suggest you review how functions work

2 Likes

Your function should only know about those variables it contains. Everything your function needs to pass this test is defined inside your function.

This is a key point in general, and why you’ll hear developers discouraging the use or reliance on global variables. As your function doesn’t own them, it can’t manage them or rely on them remaining consistent.

You are using myObj, but within the managed scope of your function, myObj doesn’t exist. It is included so you, as the developer, can get a sense of the shape your data might take, but it’s only for reference, not implementation.

1 Like

Hi snowmonkey and ilenia
I don’t know if is the best way but I put the var inside my function and change the id regarding my var test = var test = obj.hasOwnProperty(checkProp);
Thank a lot for your reactivity and your advice

The var is already in your function. You don’t have to work at getting it there. Wherever you refer to the global myObj within your function, change it to refer to obj instead. That’s the reference your function contains, and the only one or should need to know about.