Today I continued to look at the “Testing Objects for Properties” challenge. I wasn’t able to determine the solution yet, but I want to study it some more and see if I can find the solution before trying one of “Get Help” resources. I decided to make an effort to better understand the lesson as well as comparing what worked and didn’t so far.
These were the notes I took paraphrased:
After objects are made, we might be curious if properties do or don’t exist within it.
This can be done with the .hasOwnProperty(propName) object method.
Depending on whether it’s found, it gives back either true or false.
An example was also given, but I included my own below.
var meals = {
breakfast: "pancakes",
lunch: "salad",
dinner: "smoked salmon"
};
/* My example differs slightly as I use the console.log command to display the result on the console */
console.log(meals.hasOwnProperty("snack")); /* false since no snack property exists */
console.log(meals.hasOwnProperty("dinner")); /* true since the object includes a dinner property */
- The question I need to answer is to modify a previously defined function to determine whether a passed object has a specific property. If there is, the value associated with that property (the value within quotation marks) needs to be returned. Otherwise, the string “Not Found” should be returned.
I tried a few different lines of code but so far haven’t found a solution that meets all six conditions. (The six conditions are three examples that return the properties, while the others return “Not Found”.) My answers either meet the first or last three. I attempted to review previous lessons related to functions to see if I could get a hint and will continue reviewing them when I go back to this problem at a later day.
Background image by Alltechbuzz on Pixabay.