JavaScript Algorithms and Data Structures testing properties

Tell us what’s happening:
Testing Objects for Properties
The var myObj seems to be missing as it is the variable containing the information:
var myObj = {
gift: “pony”,
pet: “kitten”,
bed: “sleigh”,
city: “Seattle”
};

I added myObj. Then the ‘Not Found’ worked. From the function line
function checkObj(Obj, checkProp) {
The arg is passed into the Obj arg, as of yet I have not found checkProp to contain data. When I put in ‘gift’ the console.log(checkObj(“gift”)); Returns pony, but the test comes back, " should return the string pony ." which the console.log indicates is being returned.

  **Your code so far**

var myObj = {

gift: "pony",

pet: "kitten",

bed: "sleigh",

city: "Seattle"

};

function checkObj(Obj, checkProp) {

var answer = “”;

console.log(myObj[Obj]);

if (myObj.hasOwnProperty(Obj)){

 console.log(myObj[Obj]);

 return myObj[Obj]; 

}

//if (myObj.hasOwnProperty(Obj)) {

// console.log(“It was true”);

// console.log(Obj);

// checkProp = myObj[Obj];

// console.log(checkProp + " the answer!");

// checkProp = myObj[Obj];

// console.log(checkProp + " is the contents of checkProp");

 // return checkProp;

else {

return "Not Found";

}

// Only change code above this line

}

console.log(checkObj(“city”));

console.log(checkObj(“gift”)+ " testing");




function checkObj(Obj, checkProp) {
// Only change code below this line
var myObj = {
  gift: "pony",
  pet: "kitten",
  bed: "sleigh",
  city: "Seattle"
};
var answer = ""; 
console.log(myObj[Obj]);
if(myObj.hasOwnProperty(Obj)){
   return myObj[Obj];

//if (myObj.hasOwnProperty(Obj)) {
 // console.log("It was true");
 // console.log(Obj);
 // checkProp = myObj[Obj];
 // console.log(checkProp + " the answer!");
 //   checkProp = myObj[Obj];
 //   console.log(checkProp + " is the contents of checkProp");
   // return checkProp;
} else { 
  return "Not Found";
} 
// Only change code above this line
}
console.log(checkObj("city"));
console.log(checkObj("gift")+ " testing");
  **Your browser information:**

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

Challenge: Testing Objects for Properties

Link to the challenge:

you don’t need to create your own object

the object changes each time and can be accessed with the Obj parameter

the property name is instead in checkProp. Obj is an object, checkProp a string

you cann the function like this, but it is instead called like checkObj({city: "Seattle"}, "city"), look at the tests

It seems what I didn’t understand at first was that when the function loads propCheck is only had data if the obj points to so an item. Not so sure I am all that clear on it now. But through a lot of banging around, plus reading about twenty web pages it works. I did this which it turns out to be what a lot of other people did. I tried a few shorter versions, but they left off the last item for some reason and after three days I just moved on.

function checkObj(obj, checkProp) {
  // Only change code below this line
 var myObj = {
   gift: "pony",
   pet: "kitten",
   bed: "sleigh",
   city: "Seattle",
   };
   
   if(obj.hasOwnProperty(checkProp)){
     return obj[checkProp];
   }else {
     return "Not Found";
   }
  
   // Only change code above this line
}
I could not get it to work without putting in myObj and I see a lot of other people did the same. I came across - https://stackoverflow.com/questions/51388562/javascript-testing-objects-for-properties - which had the same thing without all of the console.logs so I used his as it was cleaner and he had a great explanation on how it worked.

Sometimes when you look up solutions on the forum to copy, you will find solutions to old versions of the challenges. This is part of why I recommend against copying other people’s solutions.

Very true as the problem I was having had two args in the function heading instead of one.

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