jeudi 17 décembre 2015

If / if else logic in javascript

var h = game.ship.hull.hullIntegrity(); //returns int from 0-100
var hstr = h.toString()+"%";
gcp.hullIntegrity.text = hstr;
gcp.hullHealth.text = game.ship.hull.health;
//colorize data
console.log(h);
if (h === 100) {
    gcp.hullIntegrity.addColor("#05AA0D",0);
} else if (75 <= h < 100) {
    console.log("yellow: ", h + " >= 75.");
    gcp.hullIntegrity.addColor("#FFEC0D",0);
} else if (25 <= h < 75) {
    console.log("red:", h + " < 75.");
    gcp.hullIntegrity.addColor("red",0);
} else if (h < 25) {
    console.log("grey");
    gcp.hullIntegrity.addColor("grey",0);
}

I am trying to colorize the display of a canvas text object based on its value. For some reason, the previous code has the following behavior:

  • When h = 100, it displays green. This is correct.
  • When h < 100, it displays yellow. This is correct.
  • When h < 75, it SHOULD display red. It displays yellow.

Console.log shows:

    70
    yellow:  70 >= 75.

But 70 is obviously less than 75, so why isn't the console displaying "red"?

Why?

Aucun commentaire:

Enregistrer un commentaire