views:

811

answers:

4

I have two form fields money and years that need to output a number into a third field cv .

Ideally while the first numbers are being entered but I'm OK with it happening after a button is pressed as well

<FORM name="salary">
    <input type="number" size=12 name="money"> 
    <input type="number" size=12 name="years"> 
    <input type="number" size=12 name="cv"> 
</FORM>

The math involved would be the following.

(money * years) - (money * years) x = cv

Where:

  • x equals 0 if years equals 1
  • x equals .2 if years equals 2
  • x equals .45 if years equals 3
  • x equals .6 if years equals 4
  • x equals .65 if years equals 5
  • x equals .75 if years equals 6
+3  A: 

You can use jQuery, in which case it'll be something like this:

var money = parseFloat($("#money").val());
var years = parseInt($("#years").val(), 10);
var x = [0.00, 0.20, 0.45, 0.60, 0.65, 0.75][years-1];

$("#cv").val((money*years)*(1-x));
Ben Alpert
Wouldn't it be more efficient to put the x values in an array and use years as an index?
Joel Potter
Maybe… : P (Thanks!)
Ben Alpert
You could also put the 1-x values into the array and lose the subtraction in the last line, which is slightly more efficient but has the disadvantage that it isn't as obvious that it follows the specification. In this tiny example I'd leave it as-is, but in a more complex case pre-processing the constants and building the table may be a win.
Berry
This answer helped me doing some maths in JavaScript. Thanks.
Kieran Andrews
+3  A: 

Breaking the problem down, you need to know how to use javascript to:

  • Programmatically get the text entered into a textbox
  • Confirm that the textbox contains numerical data
  • Perform the math
  • Set the value of the third textbox

All that and much more can be found here.

Dave Swersky
A: 

Your going to want to put an onKeyUp() event on both of the fields people enter the data into and call a function something like what follows, in addition add an id to each field as used in the function. If you are not using prototype or jquery you need to use getElementById('var') in place of the $('var')

function calculateAndSetCV()
{
    money = parseFloat($('money').val());
    years = parseInt($('years').val());
    cv = 0;

    if(years == 1)
    {
        cv = (money * years) - (money * years)0;
    }

    $('cv').val(cv);
}
Andrew Clark
A: 

Do the math:

(money * years) - (money * years) x = cv
(money * years) x = (money * years) - cv
x = ((money * years) - cv) / (money * years)
x = 1 - cv / (money * years)

Get jQuery at http://jquery.com/

Update your code like this (notice that name → id):

<script type="text/javascript" src="jquery.js"></script>
<input type="number" size="12" id="money" />
<input type="number" size="12" id="years" />
<input type="number" size="12" id="cv" />
<div id="result"></div>
<script type="text/javascript">
  $ ('input').bind ('input change keyup keydown keypress', function () {
    $ ('#result').val (
       1 - $ ('#cv').val () / ($ ('#money').val () * $ ('#years').val ())
    )
  })
</script>
Ilya Birman