Testing Objects for Properties "Not Found" error

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");

EDIT:
I’ve forgotten about the second “=”

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.

markdown_Forums

This doing an assignment, not a comparison. Assignments are truthy.

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.

1 Like

Hi,

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.

At the risk of being pedantic…

Only assignments to a truthy value are truthy. The assignment just returns the value that was assigned:

let a = true;
if (a = false) alert(0) //nothing happens

It’s only functionally the same if res is a boolean. if (res) will run if res is any truthy value.

1 Like

Good points. I was only thinking of this example case. @lionel-rowe’s explanation is much better.

Thanks, you both helped me a lot.
It’s all quite confusing at the beginning.
Hope it gets easier;)