# Thread: Newbie having trouble with 'while loop'

1. Registered User
Join Date
Nov 2012
Posts
4

## Newbie having trouble with 'while loop'

Really getting frustrated with this homework assignment.
Screwed up my entire weekend and it is due tomorrow.
I have this script starting over so that another food bill could be tried again.
The problem has been that to opt out of the script and discontinue the viewer must type -999
Code:
```main();
function main()
{
var bill = purchasePrice();  		 //price on the food bill
caculateTotalBill(bill);		 //Tip plus food bill

}

function purchasePrice()
{
pp = parseFloat(prompt("Input how much your meal purchase is. To exit type -999", ""));
var MINPRICE = .10;
var MAXPRICE = 1000;

//Validate the inputted pp
if (pp = -999)
{exit}

while((isNaN(pp)) || (pp < MINPRICE) || (pp > MAXPRICE))

{
pp = parseFloat(prompt("Input how much your meal purchase is", ""));
}

return pp;
}

function caculateTotalBill(bill)
{
//MODULE caculateTotalBill()
var	TIP	=  0.15;				 //Going rate for tip
var TAX = 	0.07;				//tax
var theTip = bill * TIP;
var theTax = bill * TAX;
var totalBill = bill + theTip + theTax;
outputInfo(bill, theTip, theTax, totalBill);

}

function outputInfo (bill, theTip, theTax, totalBill)
{
alert("Meal purchase price: " + "\t\t" + bill.toFixed(2) + "\n" +
"Tip:  " + "\t\t\t\t\t\t" + theTip.toFixed(2) + "\n" +
"Tax:  " + "\t\t\t\t\t" + theTax.toFixed(2) + "\n" +
"Your total bill will be: " + "\t\t" + totalBill.toFixed(2));
start(bill);
}
function start(bill)
{
while (bill != -999)
{
main();
}
}```

2. Registered User
Join Date
Oct 2010
Location
Versailles, France
Posts
1,290
exit seem to be PHP ? return could be better !

3. Registered User
Join Date
Nov 2012
Posts
4
It's a javascript class.
I just talked to a student that said I could use "Do you want to end this script? Type Y/N"

4. Registered User
Join Date
Oct 2010
Location
Versailles, France
Posts
1,290
Then this would be better !
Code:
`If (pp==-999) {exit}`

5. Registered User
Join Date
Nov 2012
Posts
4

## What if they want to run again?

Originally Posted by 007Julien
Then this would be better !
Code:
`If (pp==-999) {exit}`
That will help me exit the program but what if they want to run it again?

6. Registered User
Join Date
Oct 2010
Location
Versailles, France
Posts
1,290
Reload the page with a link, a button, or display a form to make your calculation with an onchange event...

7. Registered User
Join Date
Nov 2012
Posts
4
Originally Posted by 007Julien
Reload the page with a link, a button, or display a form to make your calculation with an onchange event...
Not quite what I was looking for but thanks anyways.
I got the script to run the right way.