samedi 9 février 2019

How to create multiple if statements in JavaScript?

I'm getting values from a dynamic JSON file (that is getting it's values as an array from a sensor through an ESP8266) and and I'm trying to get a different word for each value of the sensor. But as I'm trying to do it, the conditional evaluates to true to multiple statements.

I need help on conditionals to get the word for each value, for example, if I get '25' as value, I need to get displayed just the word 'sad', not the words 'anxious' or 'happy'. If I get the value '37', I want just 'happy' to be displayed.

Here's the code:

if (val0 > 40){
 fill(255,0,0);
 textSize(15);
 text('anxious',width/2-50,70); 

} else if (val0 > 35) {
 fill(255,200,0);
 textSize(15);
 text('happy',width/2-50,70); 

} else if (val0 > 20){
 fill(0,0,255);
 textSize(15);
 text('sad',width/2-50,70); 

} else {
 fill(255);
 textSize(15);
 text('nothing',width/2-50,70); 
}

As you can see, I'm completely new on js, so please guide me a little!

Aucun commentaire:

Enregistrer un commentaire