I have the below if statement in use to compare 2 rows in the same column in a table. I use the same block of code multiple times to perform the same calculation on different columns.
Basically, I want to have an arrow show both the movement in value between one row and the next, as well as the threshold the value falls into. So, for example, in my code below anything above 15 should be a red arrow and if the previous value was lower then the arrow should point up. I render the arrows using images called from another location later in my code, so the if statement just gives me the string for the appropriate arrow.
As I said above, I have the same block of code numerous times. The strange thing is, the output works for some variables and does’t for others; even though it’s the exact same code with the variable names replaced! It gives me the wrong are SOMETIMES for no apparent reason.
Do I have too many conditions? Is there a more efficient way to do what I’m trying to do?
Any assistance would be greatly appreciated!
Thanks,
Karl
var Arrow = " ";
if(KPI[0] > KPI[1] && KPI[0] >= 15)
{
Arrow = "redarrowup.png";
}
else if(KPI[0] < KPI[1] && KPI[0] >= 15)
{
Arrow = "redarrowdown.png";
}
else if(KPI[0] = KPI[1] && KPI[0] >= 15)
{
Arrow = "redarrowflat.png";
}
else if(KPI[0] > KPI[1] && KPI[0] >= 10 && KPI[0] < 15)
{
Arrow = "yellowarrowup.png";
}
else if(KPI[0] < KPI[1] && KPI[0] >= 10 && KPI[0] < 15)
{
Arrow = "yellowarrowdown.png";
}
else if(KPI[0] = KPI[1] && KPI[0] >= 10 && KPI[0] < 15)
{
Arrow = "yellowarrowflat.png";
}
else if(KPI[0] > KPI[1] && KPI[0] < 10)
{
Arrow = "greenarrowup.png";
}
else if(KPI[0] < KPI[1] && KPI[0] < 10)
{
Arrow = "greenarrowdown.png";
}
else
{
Arrow = "greenarrowflat.png";
}
At first glance I’d say that you have an “=” where you need an “==” (or even “===”). For example
should probably be
(or even === as I said). And so forth. “=” is an assignment operator, it sets the value of KPI[0] to the value of KPI[1] and then returns that value, so you are essentially doing the following:
which I doubt is the intention. That said, as several others have already said there are better ways to structure the code.