Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

Hi there, I'm trying to figure this out and hoping someone can assist so I can use it as a base for my calculations.

If the number entered is less than 30 and 'one course' is checked, the cost pp should be \$15. If the number entered is less than 30 and 'two course' is checked, the cost pp should be \$20. If the number entered is more than 30 and 'one course' is checked, the cost pp is \$25 and so on.

I got got to here so far..

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<title>BuffetQuote 2012 </title>
<script type="text/javascript">

function btncheckform_onclick()
{

var cost = document.form1.numberofpeople.value;
var course = document.form1.course.checked;
var tlcost =
if (cost<30 && course.checked) = true;
then document.form2.totalcost.value=tlcost;

}

</script>
<body>
<form action="script.url"name="form1">

Number of people: <input type="text" name="numberofpeople" onblur ="numberofpeople_onblur()" size ="30"
/> <br />

<h1>Select number of courses required</h1>
<input type="radio" name="course" value="one" /> One Course
<input type="radio" name="course" value="two" /> Two Course
<br /><br />

<input type="button" value="Submit Quote"
name= "btncheckform" onclick="btncheckform_onclick()"/>
<input type="reset" value="Reset Quote"/>

</form>
<form action="scripting.url"name="form2">

Total Cost: <input type="text" name="tlcost" size="30";
/> <br />
</form>
</body>
</html>

• Several errors I am afraid. You need to revise your understanding of if...else (not if...then). And include more user input validation.

This ought to move you forward. Please study it and learn from it. It is not really in your best interests that others do your all or most homework for you. Your teacher may gain a false and exaggerated idea of your programming capabilities and so not offer you the support you need. Also, if you hand in other people's work which you do not completely understand, then you will start to fall behind and your difficulties will increase.

Code:
```<html>

<body >

<form name = "form1" action = "">

<h3>Number of people (1-60): <input type="text" name="numPeople" size ="2" maxlength = "2"></h1>

<h3>Select number of courses required</h3>
<input type="radio" name="course" value="1" /> One Course
<input type="radio" name="course" value="2" /> Two Course
<br /><br />

<input type="button" value="Submit Quote"  onclick="checkForm()"/>
<input type="reset" value="Reset Quote"/>

<h3>Total Cost: <input type="text" name="tlcost" size="6" readonly;</h3>

</form>

<script type="text/javascript">

function checkForm() {
var people = Number(document.form1.numPeople.value) || 0;   // 0 if NaN entered
if ((people <= 0) || (people >60)) {
alert ("You must enter a number from 1 to 60");
document.form1.numPeople.value = ""; // clear the field
return false;  // go no further
}

var  c= 0;
var f = document.form1.course;  // avoid repetition
for (var i =0; i < f.length; i++) {  // loop through the radio buttons to determine which one checked
if (f.course[i].checked) {
c = f.course[i].value;  // 1 or 2, not "one or "two" as you want to use this value in a subsequent calculation
}
}
if (c ==0) {  // no radio selected
alert ("You must select one or two courses");
return false;
}

var tot = 0;

//   Now do your calculations of cost-per-head based on people and courses to arrive at the total cost
//  I don't understand why the cost per head is greater if the number of people is over 30.

document.form1.tlcost.value = tot.toFixed(2);

}

</script>
<body>

</html>```
A tip is to use the shortest names for functions and variables which are still meaningful/comprehensible, not something like btncheckform_onclick which is unwieldy and prone to typing errors. Rather than using underscores _ it is prefered to use camelCase.

It is recommended that you place the opening brace following the function, if, else, for, while, do, switch, and try statements on the same line and not on the following line. Apart from that every Javascript statement should be followed by a semi-colon (;). It is quite possible to disregard this advice, but if you do one day it will rise up and bite you in the undercarriage.

BTW, when posting here please help us to help you by following the posting guidelines and wrapping your code in CODE tags. This means use the octothorpe or # button on the toolbar. You can (and should) edit your previous post.

"If you can't explain it simply, you don't understand it well enough”
“Everything should be as simple as it is, but not simpler.”
- both quotes Albert Einstein (German born American Physicist who developed the special and general theories of relativity. Nobel Prize for Physics in 1921. 1879-1955)

• Hi Philip,
Thanks for your response. I've studied the code overnight and see I had completely gone wrong. I was confused with if/else/then/do/while.

I'm still unsure how to do the calculations at the end. Can you give me an example? Is it something like this..

var totppl = document.form1.numPeople.value
var coursechoice = document.form1.course.value
var tot = numPeople*15
var = if (numPeople <=30 && numPeople ==1)
{document.write (tot);
}
else if (numPeople <=30 &&numPeople==1)
{document.write (tot);
}

var totalcost = numberofpeople*30", or something similar, instead of "var totalcost = prompt(numberofpeople*30)"?

<form name = "form1" action = "">

<h3>Number of people (1-60): <input type="text" name="numPeople" size ="2" maxlength = "2"></h1>

<h3>Select number of courses required</h3>
<input type="radio" name="course" value="1" /> One Course
<input type="radio" name="course" value="2" /> Two Course
<br /><br />

<input type="button" value="Submit Quote" onclick="checkForm()"/>
<input type="reset" value="Reset Quote"/>

<h3>Total Cost: <input type="text" name="tlcost" size="6" readonly;</h3>

</form>

<script type="text/javascript">

function checkForm() {
var people = Number(document.form1.numPeople.value) || 0; // 0 if NaN entered
if ((people <= 0) || (people >60)) {
alert ("You must enter a number from 1 to 60");
document.form1.numPeople.value = ""; // clear the field
return false; // go no further
}

var c= 0;
var f = document.form1.course; // avoid repetition
for (var i =0; i < f.length; i++) { // loop through the radio buttons to determine which one checked
if (f.course[i].checked) {
c = f.course[i].value; // 1 or 2, not "one or "two" as you want to use this value in a subsequent calculation
}
}
if (c ==0) { // no radio selected
alert ("You must select one or two courses");
return false;
}

var tot = 0;

• I think I have done enough here - otherwise it would amount to doing your homework for you, which is discouraged in this forum.

Frankly, if you cannot now calculate the total cost based on the already captured number of people (variable name people) and number of courses (variable name course) then you need to revise your understanding of Javascript seriously or take up underwater motorcycling instead. I gave you

Code:
```var tot = 0;

//   Now do your calculations of cost-per-head based on people and courses to arrive at the total cost
//  I don't understand why the cost per head is greater if the number of people is over 30.

document.form1.tlcost.value = tot.toFixed(2);```
It is depressing that you seem to want to use the utterly obsolete document.write(). document.write() statements must be run before the page finishes loading. Any document.write() statement that runs after the page finishes loading will create a new page and overwrite all of the content of the current page (including the Javascript which called it). So document.write() is at best really only useful to write the original content of your page. It cannot be used to update the content of your page after that page has loaded.

var = if (numPeople <=30 && numPeople ==1) // is simply rubbish. If a number is equal to 1 it must be less than 30. And I see nowhere anything about costs for 1 person.
So is var totalcost = prompt(numberofpeople*30)

There is no if...then construct in Javascript. So how can you "confuse" it with do...while?

You seem to have ignored my advice regarding the position of opening braces and use of semi-colons. Why should I waste more time on this?

• Yes, I know I should know how to do it but I don't. Using your example is helping me build the rest of the project, this is just one small piece in it. I'm trying to get my head around the logic. I see I made a few silly errors with my code, I have altered it below and would really appreciate your help. Sorry for being a pain, I'm only learning.

var people = document.form1.numPeople.value;
var courseChoice = document.form1.course.value;
var totOnecourse = people*15;
var totTwocourse = people*20;
var = if (numPeople <=30 && courseChoice ==1);
{totOnecourse}
else if (numPeople <=30 &&courseChoice==2)
{totTwocourse}

document.form1.tlcost.value = tot.toFixed(2);

• Ok so I'm pretty sure my error lies here - {totOnecourse}

Can anyone assist?

• Originally Posted by Liz.2012
Ok so I'm pretty sure my error lies here -
var = if (numPeople <=30 && courseChoice ==1);
{totOnecourse}
I think you are in serious trouble and seem to be very confused in your understanding of the basics of Javascript coding, such as the use of the var keyword. I don't see these as "silly errors" - I see these as lack of comprehension. And for the second time you seem to have ignored my advice regarding the position of opening braces and use of semi-colons. Why should I waste more time on this?

In the code I gave you in Post #2
people is the number of people (1-60)
c is the number of courses (1 or 2).

So there are four possible outcomes:-

people <=30 and c == 1
people >30 and c == 1
people <=30 and c == 2
people >30 and c == 2

so

if (people <=30 && c == 1) {
tot = people * 15; // or whatever the cost per head is.
}
if (people <= 30 && c == 2) {
tot = people * 25; // or whatever the cost per head is.
}

One more time, it is not really in your best interests that others do your all or most homework for you. Your teacher may gain a false and exaggerated idea of your programming capabilities and so not offer you the support you need. Also, if you hand in other people's work which you do not completely understand, then you will start to fall behind and your difficulties will increase.

I must ask to excused from taking this topic any further. Discuss your problems with your teacher.

• Thanks for your help - and looks like I'm back at square one with learning!

•

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•