Click to See Complete Forum and Search --> : Need Some Help With a Function PLZ..


Lost_comp
07-16-2003, 06:19 PM
Write a function that will calculate and return the amount that should be paid as a tip for a restrurant bill.

Assume 20%

Use input box 4 user input. :confused: Im lost already.

Thanks in advanced!!

Khalid Ali
07-16-2003, 06:30 PM
get value from the text field

var val = (parseInt(document.formName.textfieldName.value)
//(presuming that values is always a number

//now calculate the tip
val = (val*.20).toFixed(2);
//if you want the tip added to the toal then

//val = (val+(val*.20)).toFixed(2);

//now re populate the text field
document.formName.textfieldName.value = val

//or see an alert box

alert("Tip to be paid = "+val)

Lost_comp
07-16-2003, 07:06 PM
coo thanks, I'll reply back if I get stuck. I think I shouldn't be though.

Lost_comp
07-16-2003, 07:13 PM
Still needing some help.....I think my whole thing is wrong.?

Khalid Ali
07-16-2003, 09:00 PM
Here you fo

<head>
<script type="text/javascript">
<!--
function Process(val){
var val = (isNaN(parseInt(val)))? 00:parseInt(val);
if(val==00){document.form1.t1.value="0"; return false;}
val = (val+(val*.20)).toFixed(2);
document.form1.total.value = val;
}

//-->
</script>
</head>
<body>
<form name="form1" action="" onsubmit="" method="post" enctype="text/plain">
Enter Amount <input type="text" name="t1" maxlength="4" onkeyup="Process(this.value);" size="4"/> * 20% = <input type="text" name="total" maxlength="4" size="4"/>
</form>

Lost_comp
07-16-2003, 09:04 PM
thanks

Khalid Ali
07-16-2003, 09:13 PM
:D

You are welcome