Hi,
I am struggling with “Testing Objects for Properties” Basic Java Script challenge.
I am failing on the third step. I don’t understand why the “Not Found” part isn’t working.
My code:
// Setup
var myObj = {
gift: "pony",
pet: "kitten",
bed: "sleigh"
};
function checkObj(checkProp) {
var res = myObj.hasOwnProperty(checkProp)
if (res = true) {return myObj[checkProp]}
else {return "Not Found"}
}
// Test your code by modifying these values
checkObj("gift");
I’ve edited your post for readability. 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 easier to read.
Note: Backticks are not single quotes.
See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.
Thank you @ArielLeslie.
Could you be so nice and help me with finding a template for the posts like this one?
I’ve seen other posts with the browser infos etc, but I couldn’t find a template.
I don’t want to make mess here.
If you click the ‘Ask for Help’ button on a challenge it will bring you through to a template. Replace the default title with a descriptive one, select the appropriate category (JavaScript, HTML, Databases, etc), answer the “Tell us what’s happening prompt” in detail.
In your case the problem is that in the first condition you are using a single (=), remember that you need to use == to compare or === for strict comparing.
Should work like this:
if (res === true) {return myObj[checkProp]}
Reminder to everyone that you never ever need to compare to true or false. if (res === true) is functionally the same as if (res), but will actually pass code review.