I don’t know why it’s not working.
At first I thought it was bcz I created a local variable instead of a global variable. But it still doesn’t work with a global variable. I tried different ways to solve it, but nothing seems to work. Maybe I’m just not understanding the question.
Could someone please explain me the whole thing?
**My code so far**
function checkObj(checkProp) {
// Only change code below this line
var obj = {
gift: "pony",
pet: "kitten",
bed: "sleigh"
};
if (obj.hasOwnProperty(checkProp)){
return obj[checkProp];
}else{
return "Not Found";
}
// Only change code above this line
}
It’s still not working . I don’t understand why.
You said I need to create a function. Does that mean that the function needs to be created inside of the checkObj() ?
Honestly I’m still confused with the question. The question was like, " Modify the function checkObj to test if an object passed to the function ( obj ) contains a specific property ( checkProp )." Why does it say " the function ( obj ) contains a specific property ( checkProp )" ? Does this mean the new function name will be the property name of the function checkObj() ?
Please explain this.
And here’s my modified code.
Right now you have a function inside of a function.
I would reset the lesson again.
The only thing you want to add is the if else statement.
That’s it
Also, please write code directly into the forum instead of posting screenshots.
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.
It worked now! You were right, all I had to do was to add the if/else statement and I made it so complex.
Here’s my working code.
function checkObj(obj, checkProp) {
// Only change code below this line
if (obj.hasOwnProperty(checkProp)){
return obj[checkProp];
}else{
return "Not Found";
}
return "Change Me!";
// Only change code above this line
}