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);
-
1The 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.user2417483– user24174832014年09月25日 06:52:16 +00:00Commented 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.Barmar– Barmar2014年09月25日 06:52:19 +00:00Commented 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... :SDwza– Dwza2014年09月25日 07:55:29 +00:00Commented Sep 25, 2014 at 7:55
3 Answers 3
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.
4 Comments
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 :)while or do-while. My change is that it should be totTickets <= MAX_SEAT_COUNT, not MAX_SEAT_COUNT <= totTicketsthere seem to be multiple problems :
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.
make sure you parseInt from the prompt response.
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
totTickets = toddlersCounter + juniorsCounter + adultsCounter;, and if you don't reset those vars except totTickets you will reach it in a few loops.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 !