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 trialbrandonlind2
7,823 PointsHow 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;}
2 Answers
Jennifer Nordell
Treehouse TeacherWell 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
7,823 Pointsok thank you, I fixed that, its still not working though
jason chan
31,009 PointsYes your using assignment which single =
vs == or ===
in your if blocks
Austin Whipple
29,725 PointsAustin Whipple
29,725 PointsFormatted your code a bit. =)