0

I'm trying to create a JavaScript lease calculator where a user will enter an input (purchase price) and based on the amount that is input will produce a calculation (which further I will add a range to go a certain percentage higher and lower than the calculated number).

Here is an example from my javascript:

<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
function aplusb(form) {

a=eval(Calc.num1.value)

if (a < 1000)
  {
  b= 0.05372
  c=a * b
  Calc.Input.value = c
  Calc.Input2.value = c - (c * 0.15)
  Calc.Input3.value = c + (c * 0.15))
  }
else if (1001 < a < 2000)
  {
  b= 0.04875
  c=a * b
  Calc.Input.value = c
  Calc.Input2.value = c - (c * 0.15)
  Calc.Input3.value = c + (c * 0.15))
  }
else
  {
  b= 0.04689
  c=a * b
  Calc.Input.value = c
  Calc.Input2.value = c - (c * 0.15)
  Calc.Input3.value = c + (c * 0.15))
  }
}

</script>

and the html i'm using to call the script

<form name="Calc" action="--WEBBOT-SELF--" method="POST">
<p>

Enter Purchase Price:
<input type="text" name="num1" size="12" tabindex="1">

<br> Calculation: <input type="text" name="Input" size="17" tabindex="4">

Range
<input type="text" name="Input2" size="17" tabindex="4">
<input type="text" name="Input3" size="17" tabindex="4">

</p>
<input type="button" name="sum"  value="Calculate" OnClick="aplusb(this.form)" tabindex="3">
</form>

The script works fine if it's just doing the same calculation on any number, but as soon as I add the "if" command, it doesn't work after that. Essentially there will be a group of about 10 if else commands. Any help would be much appreciated! Thanks!

Linda C
  • 3
  • 3
  • 4
    You seem to have forgotten something very important. Can I remind you to add in the JavaScript, and the relevant HTML? Remember, we like [SSCCE code](http://sscce.org/). And [live demos](http://jsfiddle.net) are nice, too. – David says reinstate Monica Jul 13 '12 at 20:55
  • I'm just in the process of adding it again! Some how when I posted the question it deleted everything after the first little paragraph! – Linda C Jul 13 '12 at 20:57
  • I cant find any example. Did you missed putting code.? Anyways what is the problem you are facing in your code. Is t not running or showing some wierd output. – Himanshu.MarJAVA Jul 13 '12 at 20:57
  • I just added a short example of the code that isn't working. I can't get it to run at all if I add the restrictions on "a" or otherwise the user inputed value. – Linda C Jul 13 '12 at 21:05

2 Answers2

3

I may be wrong but I don't think this is valid in JavaScript

else if (1001 < a < 2000)

I think it should be

else if (a > 1001 && a < 2000)

Please someone correct me if I am wrong.

AverageMarcus
  • 893
  • 8
  • 23
0

Your main problem is the lack of semicolons and the lines with double )) such as:

Calc.Input3.value = c + (c * 0.15))

Fixed with:

Calc.Input3.value = c + (c * 0.15);

Here is a fixed version: http://jsbin.com/atelen/edit#preview

Kieran
  • 5,646
  • 3
  • 22
  • 32
  • Semicolons are optional in `javascript`. – The Alpha Jul 13 '12 at 21:11
  • 1
    Indeed, but my second point still stands, it's failing due to the )) – Kieran Jul 13 '12 at 21:12
  • Thank you Kieran! I haven't done too much javascript for a while and this seems to have fixed the problem I was having! The only other quick thing would be rounding off the calculated numbers to two decimal places. – Linda C Jul 13 '12 at 21:15
  • As Sheikh says, toFixed should work: [As answered here](http://stackoverflow.com/questions/1726630/javascript-formatting-number-with-exactly-two-decimals?lq=1) – Kieran Jul 13 '12 at 21:20
  • I will try and figure that out! Thank you very much! – Linda C Jul 13 '12 at 21:29