www.webdeveloper.com
Results 1 to 7 of 7

Thread: Newbie having trouble with 'while loop'

Hybrid View

  1. #1
    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))
    	
    	{		  
    		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!");
    }

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

  3. #3
    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. #4
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,271
    Then this would be better !
    Code:
    If (pp==-999) {exit}

  5. #5
    Join Date
    Nov 2012
    Posts
    4

    What if they want to run again?

    Quote Originally Posted by 007Julien View Post
    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. #6
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,271
    Reload the page with a link, a button, or display a form to make your calculation with an onchange event...

  7. #7
    Join Date
    Nov 2012
    Posts
    4
    Quote Originally Posted by 007Julien View Post
    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.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles