# Newbie having trouble with 'while loop'

• 11-19-2012, 10:10 AM
Terri530
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))                 {                                  alert("Invalid purchase price inputted. Please reenter.");                 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(); } alert("Thanks! See ya!"); }```
• 11-19-2012, 10:19 AM
007Julien
exit seem to be PHP ? return could be better !
• 11-19-2012, 10:32 AM
Terri530
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"
• 11-19-2012, 11:50 AM
007Julien
Then this would be better !
Code:

`If (pp==-999) {exit}`
• 11-19-2012, 12:10 PM
Terri530
What if they want to run again?
Quote:

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?
• 11-19-2012, 03:17 PM
007Julien
Reload the page with a link, a button, or display a form to make your calculation with an onchange event...
• 11-19-2012, 05:56 PM
Terri530
Quote:

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.

## X vBulletin 4.2.2 Debug Information

• Page Generation 0.14867 seconds
• Memory Usage 2,364KB
• Queries Executed 11 (?)
Template Usage (21):
• (3)bbcode_code_printable
• (2)bbcode_quote_printable
• (1)footer
• (1)gobutton
• (1)navbar_moderation
• (1)navbar_noticebit
• (2)option
• (1)spacer_close
• (1)spacer_open

Phrase Groups Available (3):
• global
• postbit
Included Files (19):
• ./global.php
• ./includes/class_bootstrap.php
• ./includes/init.php
• ./includes/class_core.php
• ./includes/config.php
• ./includes/functions.php
• ./includes/class_friendly_url.php
• ./includes/class_hook.php
• ./includes/class_bootstrap_framework.php
• ./vb/vb.php
• ./vb/phrase.php
• ./includes/functions_calendar.php
• ./includes/class_bbcode_alt.php
• ./includes/class_bbcode.php
• ./includes/functions_bigthree.php
• ./includes/functions_notice.php

Hooks Called (41):
• init_startup
• init_startup_session_setup_start
• database_pre_fetch_array
• database_post_fetch_array
• init_startup_session_setup_complete
• global_bootstrap_init_start
• global_bootstrap_init_complete
• cache_permissions
• fetch_foruminfo
• global_state_check
• global_bootstrap_complete
• global_start
• style_fetch
• global_setup_complete
• bbcode_fetch_tags
• bbcode_create
• bbcode_parse_start
• cache_templates
• cache_templates_process
• template_register_var
• template_render_output
• fetch_template_start
• fetch_template_complete
• parse_templates