0

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 Answers3

1
  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){

Banning
  • 2,279
  • 2
  • 16
  • 20
  • Thanks, removing the comma and addressing the {} on the last statement allowed for the 1st and last statement to be recognized, but...the 2nd middle statement isn't being read? – Brooke Rogers Jul 19 '13 at 21:38
  • Did you remove the comma's from everything first? so all your numbers look like `1000000`? ... also try doing a `console.log(annualUsage.rawValue)` and see what the result is in firebug – Banning Jul 19 '13 at 21:53
  • yup it's not reading my (annualUsage.rawValue >= 1000000 && annualUsage.rawValue < 10000000) statement..any ideas? – Brooke Rogers Jul 19 '13 at 21:54
  • First try to `console.log(annualUsage.rawValue)` and see what that spits out because the rest of your statements look fine to me. – Banning Jul 19 '13 at 21:56
  • if says that annualUsage is not defined but the other statements work? – Brooke Rogers Jul 19 '13 at 21:58
  • LOL AWESOME I love stuff like that LOL... do you think you could put an example of this up on http://jsfiddle.net/ and get it to break? Then we could go from there. – Banning Jul 19 '13 at 22:00
  • I've never used jsfiddle.net does it work with pdf forms? I'm not seeing it. You can see a copy of the pdf here: http://c2intl.com/recquotesheet.pdf – Brooke Rogers Jul 19 '13 at 22:11
  • As you can see in this fiddle your code is correct http://jsfiddle.net/VsW45/ ... by changing the value of `annualUsage.rawValue` you can see teh different results. The issue lies in how that variable is being populated which may be another ticket/question you'll have to make on stackoverflower as this question pertaining to your javascript is `if/elseif` statements are correct. – Banning Jul 22 '13 at 13:36
0

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.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
0
  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) 
    
AllTooSir
  • 48,828
  • 16
  • 130
  • 164