Enter the number of bills and coins and this script will calculate the total amount of money you have. All blank entries are ignored. Input field validation is also included -- try entering a non-digit into a field. Also, this script is a good demonstration of a JavaScript return function in use.
Add the below code to the <body> section of your page:
<scriptlanguage="javascript"type="text/javascript"> /* Visit http://www.yaldex.com/
for full source code
and get more free JavaScript, CSS and DHTML scripts! */
<!-- Begin function
s(num,
val){ amount =
num *1;// amount is the num or NaN sum =(!num
?0:
num)*
val;// the sum for that bill or coin
if(isNaN(amount)){// if the entire is not a number
alert( "' "+
num +" ' is not a valid entry and that
field will " +"not be included in the total
money calculation." );
return0;
} else
return sum;// if it is OK, send sum back }
function
money(form){ hun =
s(form.hun.value,100);// Each amount is the returned
value fif =
s(form.fif.value,50);// for the amount in the s()
function twe =
s(form.twe.value,20); ten =
s(form.ten.value,10); fiv =
s(form.fiv.value,5); one =
s(form.one.value,1); hlf =
s(form.hlf.value,.5); qtr =
s(form.qtr.value,.25); dme =
s(form.dme.value,.1); nck =
s(form.nck.value,.05); pny =
s(form.pny.value,.01);
// add up all the amounts var
ttl =
hun +
fif +
twe +
ten +
fiv +
one +
hlf +
qtr +
dme +
nck +
pny;