banjax
06-23-2009, 02:28 PM
Hello,
I am wondering how I would loop a calculation statement on the following JavaScript, so that the user would only have to click once to retrieve all the calculations?
Some calculations require previous results to continue and currently I can only achieve this by clicking the submit form button 4 times to complete the calculations. I'm sure there is a way of completing such but am unsure, as I'm so new to JavaScript.
Any help or further direction would be great, as I feel I've hit a brick wall.:eek: Comment back if you need any further elaboration.
Thanks in advance.:thumbsup:
<script language="JavaScript">
// script to convert number to thousandths, currency and decimal places
String.prototype.reverse = function () {return this.split('').reverse().join('')};
function Dollars (as) {this.ammount = typeof as == 'number' ? as : Number(as.toString().replace(/[$,]/g, ''))};
Dollars.prototype.valueOf = function () {return this.ammount};
Dollars.prototype.toString = function () {
if (isNaN (this.ammount)) return NaN.toString();
var n = this.ammount.toFixed(2).split ('.');
return [(this.ammount < 0 ? '-' : ''), '$', (n[0].length > 3 ? n[0].reverse().match(/\d{1,3}/g).join(',').reverse() : n[0]),'.', n[1]].join('');
}
// end thousandths, currency and decimal places script
// begin 'showpay' calculation script
function showpay() {
// validation for data entry
if ((document.calc.a.value == null || document.calc.a.value.length == 0) ||
(document.calc.b.value == null || document.calc.b.length == 0) ||
(document.calc.c.value == null || document.calc.c.value.length == 0) ||
(document.calc.d.value == null || document.calc.d.value.length == 0) ||
(document.calc.e.value == null || document.calc.e.value.length == 0))
// display alert if not
{ alert("Please enter all values");
}
else
// declare variables, if all data required is entered
{
var aa = document.calc.a.value;
var ab = document.calc.b.value;
var ac = document.calc.c.value;
var ad = document.calc.d.value;
var ae = document.calc.e.value;
var af = document.calc.f.value;
var ag = document.calc.g.value;
var ah = document.calc.h.value;
var ai = document.calc.i.value;
var aj = document.calc.j.value;
var ak = document.calc.k.value;
var al = document.calc.l.value;
var am = document.calc.m.value;
var an = document.calc.n.value;
var ao = document.calc.o.value;
var ap = document.calc.p.value;
var aq = document.calc.q.value;
var ar = document.calc.r.value;
var as = document.calc.s.value;
var at = document.calc.t.value;
var au = document.calc.u.value;
var av = document.calc.v.value;
var aw = document.calc.w.value;
// calculations
document.calc.f.value = ab * ac;
document.calc.g.value = ((ac * ad).toFixed( 2 ));
document.calc.h.value = ((ae / ag).toFixed( 2 ));
document.calc.i.value = ((ah / 4.3).toFixed( 2 ));
document.calc.j.value = ((ai / 4.3).toFixed( 2 ));
document.calc.k.value = ((ah / 52).toFixed( 2 ));
document.calc.l.value = aa * 1;
document.calc.m.value = new Dollars(aa * af);
document.calc.n.value = ad * af;
document.calc.o.value = new Dollars(aa * ag);
document.calc.p.value = aa * 4.3;
document.calc.q.value = new Dollars(af * ap);
document.calc.r.value = ad * ap;
document.calc.s.value = new Dollars(ag * ap);
document.calc.t.value = ap * 12;
document.calc.u.value = new Dollars(af * at);
document.calc.v.value = ad * at;
document.calc.w.value = new Dollars(ag * at);
}
}
// end showpay script
</script>
I am wondering how I would loop a calculation statement on the following JavaScript, so that the user would only have to click once to retrieve all the calculations?
Some calculations require previous results to continue and currently I can only achieve this by clicking the submit form button 4 times to complete the calculations. I'm sure there is a way of completing such but am unsure, as I'm so new to JavaScript.
Any help or further direction would be great, as I feel I've hit a brick wall.:eek: Comment back if you need any further elaboration.
Thanks in advance.:thumbsup:
<script language="JavaScript">
// script to convert number to thousandths, currency and decimal places
String.prototype.reverse = function () {return this.split('').reverse().join('')};
function Dollars (as) {this.ammount = typeof as == 'number' ? as : Number(as.toString().replace(/[$,]/g, ''))};
Dollars.prototype.valueOf = function () {return this.ammount};
Dollars.prototype.toString = function () {
if (isNaN (this.ammount)) return NaN.toString();
var n = this.ammount.toFixed(2).split ('.');
return [(this.ammount < 0 ? '-' : ''), '$', (n[0].length > 3 ? n[0].reverse().match(/\d{1,3}/g).join(',').reverse() : n[0]),'.', n[1]].join('');
}
// end thousandths, currency and decimal places script
// begin 'showpay' calculation script
function showpay() {
// validation for data entry
if ((document.calc.a.value == null || document.calc.a.value.length == 0) ||
(document.calc.b.value == null || document.calc.b.length == 0) ||
(document.calc.c.value == null || document.calc.c.value.length == 0) ||
(document.calc.d.value == null || document.calc.d.value.length == 0) ||
(document.calc.e.value == null || document.calc.e.value.length == 0))
// display alert if not
{ alert("Please enter all values");
}
else
// declare variables, if all data required is entered
{
var aa = document.calc.a.value;
var ab = document.calc.b.value;
var ac = document.calc.c.value;
var ad = document.calc.d.value;
var ae = document.calc.e.value;
var af = document.calc.f.value;
var ag = document.calc.g.value;
var ah = document.calc.h.value;
var ai = document.calc.i.value;
var aj = document.calc.j.value;
var ak = document.calc.k.value;
var al = document.calc.l.value;
var am = document.calc.m.value;
var an = document.calc.n.value;
var ao = document.calc.o.value;
var ap = document.calc.p.value;
var aq = document.calc.q.value;
var ar = document.calc.r.value;
var as = document.calc.s.value;
var at = document.calc.t.value;
var au = document.calc.u.value;
var av = document.calc.v.value;
var aw = document.calc.w.value;
// calculations
document.calc.f.value = ab * ac;
document.calc.g.value = ((ac * ad).toFixed( 2 ));
document.calc.h.value = ((ae / ag).toFixed( 2 ));
document.calc.i.value = ((ah / 4.3).toFixed( 2 ));
document.calc.j.value = ((ai / 4.3).toFixed( 2 ));
document.calc.k.value = ((ah / 52).toFixed( 2 ));
document.calc.l.value = aa * 1;
document.calc.m.value = new Dollars(aa * af);
document.calc.n.value = ad * af;
document.calc.o.value = new Dollars(aa * ag);
document.calc.p.value = aa * 4.3;
document.calc.q.value = new Dollars(af * ap);
document.calc.r.value = ad * ap;
document.calc.s.value = new Dollars(ag * ap);
document.calc.t.value = ap * 12;
document.calc.u.value = new Dollars(af * at);
document.calc.v.value = ad * at;
document.calc.w.value = new Dollars(ag * at);
}
}
// end showpay script
</script>