0

I have a problem in my loop. I want to keep asking the user which ticket type to purchase and how many they would like to buy until MAX_SEAT_COUNT <= totTickets. My code would only run through the questions once. MAX_SEAT_COUNT = 2200

        do {
            ticketType = prompt ("Ticket Types: Toddlers = 1  Juniors = 2  Adults = 3 Please enter a ticket type: 1, 2, 3", "");
            if (ticketType == 1) {
                manyToddlers = prompt ("How many toddler tickets are you purchasing? You can only buy 10 tickets per ticket type.", "");
            } else if (ticketType == 2) {
                manyJuniors = prompt ("How many junior tickets are you purchasing? You can only buy 10 tickets per ticket type.", "");                  
            } else if (ticketType == 3) {
                manyAdults = prompt ("How many adult tickets are you purchasing? You can only buy 10 tickets per ticket type.", "");        
            } else {
                alert ("Please enter the correct number for each ticket type.");
            }

                if (manyToddlers <= 10) {
                    toddlersCounter = toddlersCounter + manyToddlers;
                    costToddlers = manyToddlers * toddlers;
                    alert ("You bought " + manyToddlers + " toddler tickets for $ " + costToddlers.toFixed(2)); 
                } else if (manyJuniors <= 10) {
                    juniorsCounter = juniorsCounter + manyJuniors;
                    costJuniors = manyJuniors * juniors;
                    alert ("You bought " + manyJuniors + " junior tickets for $ " + costJuniors.toFixed(2));    
                } else if (manyAdults <= 10) {
                    adultsCounter = adultsCounter + manyAdults;
                    costAdults = manyAdults * adults;
                    alert ("You bought " + manyAdults + " adult tickets for $ " + costAdults.toFixed(2));   
                } else {
                    alert ("You can only buy 10 tickets per ticket type.");
                }

        totTickets = toddlersCounter + juniorsCounter + adultsCounter;
        totSales = costToddlers + costJuniors + costAdults;     

        } while (MAX_SEAT_COUNT <= totTickets);
3
  • 1
    The prompt command always returns a string value so you should convert these text numbers to proper numerals by manyToddlers = parseInt( manyToddlers,10). The maths may also work as expected. Commented Sep 25, 2014 at 6:52
  • Don't you have the condition backwards? You should loop while the number of tickets is less than the maximum, not while it's greater. Commented Sep 25, 2014 at 6:52
  • looping to 2200 with max 10 steps per run and 3 alerts is really not recommended :) this would be 660 alerts to do... :S Commented Sep 25, 2014 at 7:55

3 Answers 3

1

Your while condition is wrong. It should be:

while (totTickets <= MAX_SEAT_COUNT)

The loop is only running once because your condition is failing after the first loop.

Sign up to request clarification or add additional context in comments.

4 Comments

Okay, I changed it and it's still asking the question one time.
Actually it doesn't matter if he uses while or do-while. The only difference is, the do-while goes through the process at least once and while firt checks conditions. And atm i can't see where he stores the data so the loop will allways start from beginning. That's the reason why it doesn't matters :)
@Dwza I didn't say anything about whether it should be while or do-while. My change is that it should be totTickets <= MAX_SEAT_COUNT, not MAX_SEAT_COUNT <= totTickets
hmm, i could swear i read some about use while not do while ^^ may i am working to long and read to much that my brain is faking me ^^
1

there seem to be multiple problems :

  1. Define all your variables and set them to a default value of 0 (or what suits you) before using them. For example if I choose adult type(3) in the first prompt, then variables manyToddlers and manyJuniors will remain undefined and now the second if(){} will throw an error of undefined in the operations.

  2. make sure you parseInt from the prompt response.

  3. change your while condition to totTickets <= MAX_SEAT_COUNT;

only as an example the code below should work fine , however i recommend you do it in a better way keeping all things clear and defined:

manyToddlers = 0;
    manyJuniors = 0;
    manyAdults = 0;

    toddlersCounter = 0;
    toddlers = 0;
    costToddlers = 0;

    juniorsCounter = 0;
    manyJuniors = 0;
    juniors =0;
    costJuniors = 0;

    adultsCounter = 0;
    costAdults = 0;
    adults = 0;

    totTickets = 0;
    MAX_SEAT_COUNT = 10;
    do {
        ticketType = prompt ("Ticket Types: Toddlers = 1  Juniors = 2  Adults = 3 Please enter a ticket type: 1, 2, 3", "");
        if (ticketType == 1) {
            manyToddlers = prompt ("How many toddler tickets are you purchasing? You can only buy 10 tickets per ticket type.", "");
        } else if (ticketType == 2) {
            manyJuniors = prompt ("How many junior tickets are you purchasing? You can only buy 10 tickets per ticket type.", "");
        } else if (ticketType == 3) {
            manyAdults = prompt ("How many adult tickets are you purchasing? You can only buy 10 tickets per ticket type.", "");
        } else {
            alert ("Please enter the correct number for each ticket type.");
        }

        manyToddlers = parseInt(manyToddlers,10);
        manyJuniors = parseInt(manyJuniors,10);
        manyAdults = parseInt(manyAdults,10);

        if (manyToddlers <= 10) {
            toddlersCounter = toddlersCounter + manyToddlers;
            costToddlers = manyToddlers * toddlers;
            alert ("You bought " + manyToddlers + " toddler tickets for $ " + costToddlers.toFixed(2));
        } else if (manyJuniors <= 10) {
            juniorsCounter = juniorsCounter + manyJuniors;
            costJuniors = manyJuniors * juniors;
            alert ("You bought " + manyJuniors + " junior tickets for $ " + costJuniors.toFixed(2));
        } else if (manyAdults <= 10) {
            adultsCounter = adultsCounter + manyAdults;
            costAdults = manyAdults * adults;
            alert ("You bought " + manyAdults + " adult tickets for $ " + costAdults.toFixed(2));
        } else {
            alert ("You can only buy 10 tickets per ticket type.");
        }

        totTickets = toddlersCounter + juniorsCounter + adultsCounter;
        totSales = costToddlers + costJuniors + costAdults;

    } while (totTickets <= MAX_SEAT_COUNT);

2 Comments

You won't get to 2200 if you keep this totTickets = toddlersCounter + juniorsCounter + adultsCounter;, and if you don't reset those vars except totTickets you will reach it in a few loops.
@user3241019 : I acknowledge and agree with what you say. This was supposed to be a help with the do while looping and so I set the MAX_TICKETS to ten. As for the actual program, a lot of improvements are required. For a start variables in loop are not required to be global and of course this is not a good way of actual ticket booking/marking system.
1

I changed your code a bit, to get it easier to read, and make it work as well. Your problem was in the structure of your while :

  • Handle the user input if it's not 1, 2, or 3
  • Get the right tickets count at the end
  • Correctly loop while totTickets <= MAX...

Here is a sample of the structure if you want to have a look :

if (manyAdults <= 10) {
   adultsCounter = adultsCounter + manyAdults;
   costAdults = manyAdults * adults;
   alert("You bought " + manyAdults + " adult tickets for $ " + costAdults.toFixed(2));
} else {
   alert("You can only buy 10 tickets per ticket type.");
}

totTickets = totTickets + toddlersCounter + juniorsCounter + adultsCounter;
//don't forget to reset their values, otherwise your ticket count will grow instantly               
toddlersCounter = 0; juniorsCounter = 0; adultsCounter = 0;
totSales = costToddlers + costJuniors + costAdults;

I put a button in the Fiddle to control the launch of your while :)

Take a look at this Working Fiddle with the entire code !

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.