Why is the javascript if else statement only register reading last else statement?

277 Views Asked by At

The javascript if else statement I am using in LiveCycle is only reading the last else statement and won’t read or recognize the statements that come before it. If you can see what I am missing please help. Here is the code:

if (annualUsage.rawValue < 1,000,000) 
{
this.rawValue = annualUsage.rawValue * 0.1;
}
else if (annualUsage.rawValue >= 1,000,000 && annualUsage.rawValue < 10,000,000) 
{
this.rawValue = annualUsage.rawValue * 0.2;
}
else (annualUsage.rawValue >= 10,000,000) 
{
this.rawValue = annualUsage.rawValue * 0.05;
}
3

There are 3 best solutions below

8
On
  1. Take the commas out of your numbers? So like

    if (annualUsage.rawValue < 1000000)

    etc...

  2. Also the last else statement doesn't get a condition so you can either remove the condition

    else{

    or turn it into an else if statement

    else if(annualUsage.rawValue >= 10000000){

0
On

You're unintentionally misusing the the comma operator.

a, b will execute both a and b and evaluate to b.

Therefore, the value you're actually passing to the ifs is 000, which is falsy.


In addition, the parenthesized expression after the else becomes a statement (through ASI) which is the body of the else clause.
The { after it starts a normal code block which always executes.

0
On
  1. Remove the comma from the numbers :

    if (annualUsage.rawValue < 1000000) 
    

    Otherwise it evaluates to an expression with comma operator.

  2. Even the last else needs to be corrected.

    else  if(annualUsage.rawValue >= 10000000)