javascript - Why does my if else statement return the incorrect result? -
i have following if else statement in main.js file:
var fixa = "trap"; var fixb = "ball"; console.log(fixa); console.log(fixb); if (fixa == "mouse" || fixa == "ball" && fixb == "mouse" || fixb == "ball") { console.log("ball hit mouse"); }else if ( fixa == "trap" || fixa == "ball" && fixb == "trap" || fixb == "ball") { console.log("ball hit trap"); }else if ( fixa == "trap" || fixa == "mouse" && fixb == "trap" || fixb == "mouse") { console.log("trap hit mouse"); } in original statement fixa , fixb variables assigned based upon objects colliding each other, did not work. kept displaying "ball hit mouse", result of if().
looking problem myself gave fixa , fixb fixed string again resulted in same output. first 2 console logs right after setting variables log correct strings. there must wrong statement itself. did reading, research, etc cant seem figure out whats wrong it.
could me?
have @ operator precendece of javascript. && operator binds stronger || operator. can circumvent behaviour adding parantheses conditions.
if ( (fixa == "mouse" || fixa == "ball") && (fixb == "mouse" || fixb == "ball")) { console.log("ball hit mouse"); }else if ( (fixa == "trap" || fixa == "ball") && (fixb == "trap" || fixb == "ball")) { console.log("ball hit trap"); }else if ( (fixa == "trap" || fixa == "mouse") && (fixb == "trap" || fixb == "mouse")) { console.log("trap hit mouse"); }
Comments
Post a Comment