/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] program not working correctly

This is a school assignment i copied from the text book, and it does not work. I am not sure if it is because of a typo in the book, or what.

The page is supposed to take your total food bill, and figure
out the tip as well as split the price for the number of guests. I can not get the Total price nor the Per Person price to work. I have been playing with this for a number of days and still can not figure out why it isnt working. Part of it is working and it looks identical to the part that is not, so it is frustrating….

Any help would be appreciated
Thanks
bandion

[code]
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml” xml:lang=”en” lang=”en”>
<head>
<meta http-equiv=”Content-type” content=”text/html;charset=UTF-8″ />
<title>
Tip Calculator
</title>
<script type=”text/javascript”>
/* <![CDATA[ */
function figureTip() {
var bill = document.forms[0].bill.value;
var tipPercent = document.forms[0].tip_percent.value;
var numPeople = document.forms[0].num_people.value;
var tipAmount = Math.round(bill * (“.” + tipPercent));
var total = Math.round(bill * (“1” + “.” + tipPercent));
var totalPerPerson = Math.round(total / numPeople);
document.forms[0].tip_amount.value = tipAmount.toLocaleString();
document.forms[0].total.value = total.toLocaleString();
document.forms[0].total_person.value = totalPerPerson.toLocaleString();
document.forms[0].total_amount.value = total.toLocaleString();
}
/* ]]> */
</script>
</head>
<body>
<script type=”text/javascript”>
<!– hide from incompatible browsers

//stop hiding —>
</script>
<h2>Tip Calculator</h2>
<form action=””>
<table border=”0″>
<tr><td>Bill: </td><td><input type=”text” name=”bill” value=”0″ onchange=”figureTip();” /></td></tr>
<tr><td>Tip &#37;: </td><td><input type=”text” name=”tip_percent” value=”0″ onchange=”figureTip();” />(enter as a whole number)</td></tr>
<tr><td># of People: </td><td><input type=”text” name=”num_people” value=”1″ onchange=”figureTip();” /></td></tr>
<tr><td>Tip amount: </td><td><input type=”text” name=”tip_amount” value=”0″ readonly=”readonly” /></td></tr>
<tr><td>Total: </td><td><input type=”text” name=”total_amount” value=”0″ readonly=”readonly” /></td></tr>
<tr><td>Total per Person:</td><td><input type=”text” name=”total_person” value=”0″ readonly=”readonly” /></td></tr>
</table>
</form>

</body>
</html>
[/code]

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@KorMay 22.2009 — use [B]onblur[/B] instead of [B]onchange[/B]

[B]onchange [/B]is a peculiar event. It occurs when a control loses the input focus [I][COLOR="Blue"]and[/COLOR][/I] its value has been modified since gaining focus.
Copy linkTweet thisAlerts:
@mrhooMay 22.2009 — Also, a script will fail when you try to read or set an undefined field's value.

You do not have an element named 'total'.

document.forms[0].total.value = total.toLocaleString();[/QUOTE]
Copy linkTweet thisAlerts:
@BandionauthorMay 22.2009 — Thankyou ... removing that line fixed the problem.

Also, thanks for editing the post and putting in the CODE tags, I will make sure I do that from now on...
×

Success!

Help @Bandion spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...