dimanche 25 septembre 2016

How can I refactor this bunch of if statements?

Imagine that I have a variable called incomingValue and I'm getting a number from an API as it's value. The values are between 0 to 1 and I'm setting two other variables depending on this value using bunch of if statements like you see below.

var incomingValue; // Set by an API
var setValueName;
var setValueIcon;
if (incomingValue < 0.10 ) { 
setValueName = "something"; 
setValueIcon ="something.png" 
}
if (incomingValue > 0.09 && incomingValue < 0.20 ) { 
setValueName = "somethingElse"; 
setValueIcon ="somethingElse.png" 
}

In the actual implementation, I have around 10 if statements checking for specific intervals up until 1. e.g. do this if it's more than 0.10 but less than 0.16 and so on.

As a JavaScript beginner it feels like this is not the right way to do things even though it gets the job done. How would I refactor this code?

Aucun commentaire:

Enregistrer un commentaire