PDA

View Full Version : Javascript Calculation help!

NewbieScott
04-21-2010, 03:00 PM
All,

Below find my JS which is supposed to do the following:
Take user supplied numerical values from 7 fields, perform the calculation function that's in the code and place it into the Total box / field.

Needless to say ...or I wouldn't be here...it doesn't work.
I thought maybe the problem was in the function, but I'm not sure

Thanks to all who can help!

NewbieScott

================================

<html>
<title>Test Prototype</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<style type="text/css">
* {
margin:0;
}
body {
background:#fff;
}
p {
margin:2em;
}
</style>
<script type="text/javascript">

function calc(D1,D2,D3,D4,D5,D6,D7){

return (((D1*2.036+D3)/(273+D5))-((D2*2.036+D4)/(273+D6)))*238*(3050/D7);

var D1=parseInt(D1.value);
var D2=parseInt(D2.value);
var D3=parseInt(D3.value);
var D4=parseInt(D4.value);
var D5=parseInt(D5.value);
var D6=parseInt(D6.value);
var D7=parseInt(D7.value);
if (isNaN(D1)) {D1=0;}
if (isNaN(D2)) {D2=0;}
if (isNaN(D3)) {D3=0;}
if (isNaN(D4)) {D4=0;}
if (isNaN(D5)) {D5=0;}
if (isNaN(D6)) {D6=0;}
if (isNaN(D7)) {D7=0;}
document.getElementById('total').value = D1+D2+D3+D4+D5+D6+D7;
}

var D1=document.getElementById('D1');
var D2=document.getElementById('D2');
var D3=document.getElementById('D3');
var D4=document.getElementById('D4');
var D5=document.getElementById('D5');
var D6=document.getElementById('D6');
var D7=document.getElementById('D7');
D1.onkeyup=function() {calc(D1,D2,D3,D4,D4,D6,D7);
D2.onkeyup=function() {calc(D1,D2,D3,D4,D4,D6,D7);
D3.onkeyup=function() {calc(D1,D2,D3,D4,D4,D6,D7);
D4.onkeyup=function() {calc(D1,D2,D3,D4,D4,D6,D7);
D5.onkeyup=function() {calc(D1,D2,D3,D4,D4,D6,D7);
D6.onkeyup=function() {calc(D1,D2,D3,D4,D4,D6,D7);
D7.onkeyup=function() {calc(D1,D2,D3,D4,D5,D6,D7);
}
</script>
<body>
<form action="#">
<p><label> Number 1: <input id="D1"></label>
<p><label> Number 2: <input id="D2"></label>
<p><label> Number 3: <input id="D3"></label>
<p><label> Number 4: <input id="D4"></label>
<p><label> Number 5: <input id="D5"></label>
<p><label> Number 6: <input id="D6"></label>
<p><label> Number 7: <input id="D7"></label>

<p>Total = <input id="total"></p>
</p>
</body>
</html>

vwphillips
04-21-2010, 04:11 PM
<html>
<title>Test Prototype</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<style type="text/css">
* {
margin:0;
}
body {
background:#fff;
}
p {
margin:2em;
}
</style>
<script type="text/javascript">

function calc(D1,D2,D3,D4,D5,D6,D7){

//return (((D1*2.036+D3)/(273+D5))-((D2*2.036+D4)/(273+D6)))*238*(3050/D7);

D1=parseInt(D1.value);
D2=parseInt(D2.value);
D3=parseInt(D3.value);
D4=parseInt(D4.value);
D5=parseInt(D5.value);
D6=parseInt(D6.value);
D7=parseInt(D7.value);
if (isNaN(D1)) {
D1=0;
}
if (isNaN(D2)) {
D2=0;
}
if (isNaN(D3)) {
D3=0;
}
if (isNaN(D4)) {
D4=0;
}
if (isNaN(D5)) {
D5=0;
}
if (isNaN(D6)) {
D6=0;
}
if (isNaN(D7)) {
D7=0;
}
document.getElementById('total').value = D1+D2+D3+D4+D5+D6+D7;
}

var D1=document.getElementById('D1');
var D2=document.getElementById('D2');
var D3=document.getElementById('D3');
var D4=document.getElementById('D4');
var D5=document.getElementById('D5');
var D6=document.getElementById('D6');
var D7=document.getElementById('D7');
D1.onkeyup=function() {
calc(D1,D2,D3,D4,D5,D6,D7);
}
D2.onkeyup=function() {
calc(D1,D2,D3,D4,D5,D6,D7);
}
D3.onkeyup=function() {
calc(D1,D2,D3,D4,D5,D6,D7);
}
D4.onkeyup=function() {
calc(D1,D2,D3,D4,D5,D6,D7);
}
D5.onkeyup=function() {
calc(D1,D2,D3,D4,D5,D6,D7);
}
D6.onkeyup=function() {
calc(D1,D2,D3,D4,D5,D6,D7);
}
D7.onkeyup=function() {
calc(D1,D2,D3,D4,D5,D6,D7);
}
}
</script>
<body>
<form action="#">
<p><label> Number 1: <input id="D1"></label>
<p><label> Number 2: <input id="D2"></label>
<p><label> Number 3: <input id="D3"></label>
<p><label> Number 4: <input id="D4"></label>
<p><label> Number 5: <input id="D5"></label>
<p><label> Number 6: <input id="D6"></label>
<p><label> Number 7: <input id="D7"></label>

<p>Total = <input id="total"></p>
</p>
</form>
</body>
</html>

mscreativedesigns
03-02-2011, 05:32 PM
Hey everyone, IM STUCK..

I have this formula that I need to turn into a javascript formula.

If (A > 25000 {
A - 25000 = B
Int(B / 50000) = C
If (C < 1) {
B / 5000 = E
E * 30 = F
If (F > 149) {
rec3Total = 149
}
} else{
(B - (C * 50000)) / 5000 = E
(C * 149) + (E * 30) = F
}
} else {
F = 0
}

This is what the formula currently is:

//Calculate Records Total
\$("[id^=rec3Total]").val("0")
}

else {
\$("[id^=rec3Total]").calc(
// the equation to use for the calculation
"Math.ceil((add3Records - 25000) / 5000) * 30",

// define the variables used in the equation, these can be a jQuery object
{