0

I found a similar question for this same assignment but that person was using a different type of loops. I have been struggling with this assignment and even with the teacher giving me pseudo code to try to explain it further I am still having difficulties with it writing out what it's supposed to in the end.

We are supposed to create an array that holds the price of theater tickets, then make a html table that has the different level of tickets and prices that correspond with the numbers in the array. After this we prompt the user for their name and validate that they did enter something. Then we are supposed to create a function named numSeats that prompts the user for the number of seats that they want to buy and validate that they entered a number and the maximum number of seats they can buy in one transaction is 6.

We are supposed to use a loop to prompt the user until they enter a valid number, then create a function named seatingChoice that prompts the user for where they would like the seats to be by indicating the correct number from the table.

seartingChoice also needs to validate that they entered a number 1-4 and use a loop to prompt the user until they enter a valid number. If the user at any time hits the cancel button in a prompt we are supposed to give an alert of "Sorry you changed your mind". This is missing from my code because i haven't figured out how to do that.

When the program calculates everything and writes to the screen in the end it is supposed to write like "UsersName ordered #tickets for a total of dollaramt" but instead it writes "Null ordered null tickets for a total of $null." The following is what i have the the javascript part of the code:

var Prices = [60, 50, 40, 30];
var Usersname = prompt("Please enter your name");
while(Usersname = null)
{
Usersname = prompt("Please enter your name");
}

function numSeats () {
var seats = prompt("Please enter the number of seats you want to buy");
parseInt(seats);
while((seats = null)||(seats > 6))
{
    seats = prompt("Please enter a number between 1 and 6"); 
    parseInt(seats);
}
    return seats;   
}
var seatswanted = numSeats ();

function seatingChoice () {
var choice = prompt("Please enter where you would like your seats to be located by indicating     the correct number from the table");
parseInt(choice)
while((choice = null)||(choice > 4))
{
    choice = prompt("Please enter a number between 1 and 4, corresponding to your section choice");
    parseInt(choice);
}
return choice;
}
var seating = seatingChoice();

var dollaramt = (seatswanted * Prices[seating-1]);



document.write(Usersname + " ordered " + seatswanted + "tickets for a total of " + "$" +  dollaramt + ".");
cbr
  • 12,563
  • 3
  • 38
  • 63
  • 1
    `while(choice == null)||(choice>6) { ..... } ` think its a typo, `while choice=null` assigns choice to null – Tejesh Nov 14 '14 at 07:21
  • On top of what the others said, parseInt(choice) does NOT assign the value back to choice. You meant to say **choice = parseInt(choice)** most likely. Same with **seats** parsing. – Alex Weinstein Nov 14 '14 at 07:38

3 Answers3

2

Instead of comparison operator you are using assignment operator:

var Usersname = prompt("Please enter your name");
while(Usersname = null)
{
Usersname = prompt("Please enter your name");
}

and prompt return empty string if user wont input anything so instead of null compare it to empty string i.e, ''. so change this to:

var userName = prompt("Please enter your name");
while(userName == '')
{
   userName = prompt("Please enter your name");
}
Zaheer Ahmed
  • 28,160
  • 11
  • 74
  • 110
0

The error I do not know why this happening, but this maybe solve your issue.

var Usersname = '';
Usersname = prompt('Choose a Username:');

this works. And you need the same for the others prompt.

Also if you want seat in rang 1-6: while((seats = null)||(seats > 6)) its wrong. Change to ((seats <= 6) && (seats > 0))

Maybe this could help you.

Best regards.

Arturo
  • 261
  • 1
  • 4
  • 19
  • 1
    *"The error I do not know why this happening, but this maybe solve your issue"*. You literally start your answer with a statement that says "I cannot answer this question". – Zeta Nov 14 '14 at 07:42
  • Also, `seats <= 6 && seats != 0` is wrong too. What about `seats = -300`? – Zeta Nov 14 '14 at 07:44
  • I only try to help him, and edited for seats = -300, thanks for the case. @Zeta – Arturo Nov 14 '14 at 07:46
0

There are several errors in your code. You can try your code using jsfiddle (you can try your code properly modified here: http://jsfiddle.net/pu3s0n50/).

Your errors are that you assign Username, seats and choice instead of comparing them, i.e.:

while(Usersname = null)

should be

while(Usersname == null)

and

while((seats = null)||(seats > 6))

should be

while((seats == null)||(seats > 6))

and finally

while((choice = null)||(choice > 4))

should be

while((choice == null)||(choice > 4))
Maffelu
  • 2,018
  • 4
  • 24
  • 35
  • Err, don't repeat yourself. That's like three times (almost) the same code. Make the problem clear, keep your answer concise. – Zeta Nov 14 '14 at 07:41
  • I'm trying to be specific and show all the errors and all the solutions since @EckoImmortal seems to be a student. – Maffelu Nov 14 '14 at 07:58
  • Awesome, thanks I knew it had to be something missing like that. Now how can I alert if cancel is pressed during one of the prompts? – EckoImmortal Nov 14 '14 at 08:01
  • @EckoImmortal: You can handle it like described in this other SO thread: http://stackoverflow.com/questions/6962718/javascript-prompt-box-cancel-button – Maffelu Nov 14 '14 at 08:06
  • @Maffelu That post does nothing but confuse me since there's already a loop response to if a prompt is answered with null – EckoImmortal Nov 14 '14 at 08:31
  • @EckoImmortal what the answer to that post states is 'In the case of Cancel, the prompt result is null, and null != '' (as per ECMA-262 Section 11.9.3).'. It then proceeds to show how to handle that situation. – Maffelu Nov 14 '14 at 08:42