Testing Objects for Properties "Not Found" error

Testing Objects for Properties "Not Found" error
0

#1

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 “=”


#2

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.


#3

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


#4

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.


#5

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.


#6

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]}


#7

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.


#8

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.


#9

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


#10

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