Imagine that I have a variable called incomingValue , and I get a number from the API as a value. Values ββrange from 0 to 1, and I set two other variables depending on this value, using a bunch of if statements, as 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 about 10 statements checking specific intervals up to 1 . for example, do this if it is greater than 0.10 but less than 0.16, etc.
As a JavaScript beginner, it seems like this is not the right way to do something, even if it does its job. How would I reorganize this code?
Update: on request, I add a complete set of intervals that are used in the source code. I have not included a complete list before, because the intervals do not match a specific pattern.
- 0 to 0.09
- from 0.09 to 0.20
- from 0.20 to 0.38
- 0.38-0.48
- 0.48-0.52
- from 0.52 to 0.62
- 0.61 - 0.80
- 0.80 to 1
javascript if-statement
cinnaroll45
source share