PDA

View Full Version : Javascript tip calculations



thats all folks
April 1st, 2007, 07:22 PM
I'm teaching my self java and have run into a bump, I was trying to make an prompt box that will ask for your bill total and then times it by .20 for the tip and display in a alert box how much the tip will be based on the bill. I tried searching google first but have had no luck.

Cheers and thanks for any input.

<script type="text/javascript" language="javascript1.5">
<!-- HIDE FROM OLDER BROWSERS
function tipCal(myTip, myBill){
var myTip = .20;
return myTip * myBill;
}
// -->
</script>
</head>
<body>
<script type="text/javascript" language="javascript1.5">
<!-- HIDE FROM OLDER BROWSERS
var yourBill=eval(prompt("How much is your bill?", ""));

var tipRate = tipCal(myTip, myBill);
//var tipRate = tipCal(yourTip, yourBill);

alert("Your tip will be: " + tipRate);

// -->
</script>

kBisk
April 1st, 2007, 08:05 PM
you'll have to do a search to find the round that you want, but heres how you'd get it to work:


<script type="text/javascript">
function calculateTipFrom (billAmount){
var tipRate = .20;
var tipAmount = billAmount * tipRate;

return tipAmount;
}

var yourBill = prompt("How much is your bill?", "");
var tipAmount = calculateTipFrom(yourBill);

alert("Your tip will be: " + tipAmount);
</script>

thats all folks
April 1st, 2007, 08:21 PM
KBisk,

Thanks soooo much!! This helps out!! I was completely off, yours makes so much sense!

Cheers