Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript

brandonlind2
brandonlind2
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.

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

Formatted your code a bit. =)

2 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
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.

brandonlind2
brandonlind2
7,823 Points

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

jason chan
jason chan
31,008 Points

Yes your using assignment which single =

vs == or ===

in your if blocks