Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial


7,823 Points

How can a create an if else statement, that says if something is a property of a specific object, do this?

for example I'm creating a simple rpg, and I have a rangedWeapons and a meleeWeapons object that contains different weapons as properties, I need for example an if statement that says if the player equipped weapon is a rangedWeapons property do this.

something like this , I tried putting just rangedWeapons for example but it obviously didnt work

update/thanks, I fixed the operator issue, however it still isn't working

I would have thought there would be a native Object method or something that compares object properties, but I'm not seeing anything. If someone can find any typos keeping this from working or point me in the direction or a native method it would great, thanks.

            else if(player.equipped.weapon===meleeWeapons){
                   player.equipped.weapon.damage += 5;}
Austin Whipple
Austin Whipple
29,725 Points

Formatted your code a bit. =)

2 Answers

Jennifer Nordell
Jennifer Nordell
Treehouse Teacher

Well for starters you're using the assignment operator (single equals) instead of a comparison operator (either double equals or triple equals). Try adjusting that and see if you can get some different results. The way you have it now it'll always evaluate to true.

7,823 Points

ok thank you, I fixed that, its still not working though

jason chan
jason chan
31,009 Points

Yes your using assignment which single =

vs == or ===

in your if blocks