jeudi 16 novembre 2017

Javascript Decimal comparison not giving desired result [duplicate]

This question already has an answer here:

I have the following JS code:

var test = {
'a': 0.1233,
'b': 2.32,
'c': 0.70567,
'd': 0.193,
'e': 3.00,
'f': 1.52
};

var command = "";

for (var key in test) {
    if ( test[key].toFixed(2) < 1.00) {
        command += "select " + key + "; " + "color green; ";
    } else if (1.00 <= test[key].toFixed(2) < 2.00) {
        command += "select " + key + "; " + "color yellow; ";
    } else if (2.00 <= test[key].toFixed(2) < 3.00) {
        command += "select " + key + "; " + "color orange; ";
    } else {
        command += "select " + key + "; " + "color red; ";
    }
}

console.log(command);

Currently the console.log is returning the following values:

select a; color green; select b; color yellow; select c; color green; select d; color green; select e; color yellow; select f; color yellow;

The correct values that I'm expecting are:

select a; color green; select b; color orange; select c; color green; select d; color green; select e; color red; select f; color yellow;

How should I change my code to make comparisons to be correct? Any help is appreciated. Thanks

Aucun commentaire:

Enregistrer un commentaire