Tell us what’s happening:
Describe your issue in detail here.
**Your code so far**
function checkObj(obj, checkProp) {
// Only change code below this line
function checkObj(obj, checkProp) {
if(obj.hasOwnProperty(checkProp)) {
return obj[checkProp];
} else {
return "Not Found";
}
}
var odj = {
'gift': 'pony',
'pet': 'kitten',
'bed': 'sleigh'
};
// Only change code above this line
}
**Your browser information:**
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.128 Safari/537.36 OPR/75.0.3969.279
Challenge: Testing Objects for Properties
Link to the challenge:
Why did you paste a function inside of a function?
Why did you paste a random object inside of the function?
Hi @derlyn.a.hernandez !
It is common for people to create their own object with this challenge.
But this is incorrect and needs to be deleted.
Here’s why you should not create a new object.
You task is to create a function that works with ANY object.
Not just one that you created yourself.
What if you had to test your function with 100 different objects.
Are you going to write out all of those objects?
No.
In the function call, that is where you will pass in an actual object and property to check to see if the property exists or not.
Here is an example
checkObj({fName:'Jess',age:'29', isAwesome:true}, 'fName')// returns Jess
checkObj({fName:'Jess',age:'29', isAwesome:true}, 'food') // returns Not Found
Edit:
Your function should work for 100’s of different objects and function calls.
Hope that makes sense!
I’ve removed the object from the primary function
and put the object checks outside
What is the primary issue with failing the check?
function checkObj(obj, checkProp) {
// Only change code below this line
function checkObj(obj, checkProp) {
if(obj.hasOwnProperty(checkProp)) {
return obj[checkProp];
} else {
return "Not Found";
}
}
// Only change code above this line
}
checkObj({gift: "pony", pet: "kitten", bed: "sleigh"}, "gift")
checkObj({gift: "pony", pet: "kitten", bed: "sleigh"}, "pet")
checkObj({gift: "pony", pet: "kitten", bed: "sleigh"}, "house")
checkObj({city: "Seattle"}, "city")
checkObj({city: "Seattle"}, "district")
checkObj({pet: "kitten", bed: "sleigh"}, "gift")
You haven’t addressed @JeremyLT 's issue of a function inside a function
Also, just to clarify, I didn’t mean for you to write all of the function calls at the bottom.
I just provided my function call example to show you why shouldn’t hardcode an object in the function because your function should work for 100’s of function calls and objects.
Hope that is clearer now.
You can choose to leave these function calls at the bottom because that isn’t the issue.
derlyn.a.hernandez:
checkObj({gift: "pony", pet: "kitten", bed: "sleigh"}, "gift")
checkObj({gift: "pony", pet: "kitten", bed: "sleigh"}, "pet")
checkObj({gift: "pony", pet: "kitten", bed: "sleigh"}, "house")
checkObj({city: "Seattle"}, "city")
checkObj({city: "Seattle"}, "district")
checkObj({pet: "kitten", bed: "sleigh"}, "gift")
Just know that you don’t have to write any for this challenge because your code is being tested behind the scenes with the FCC tests.
1 Like
system
Closed
November 28, 2021, 11:55am
7
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.