Click to See Complete Forum and Search --> : array problems


jmassengill
03-06-2003, 09:12 AM
I am trying to write a short javascript program for a college class. It is a program that is an airline reservation system for a small airline. The aircraft in question has 10 seats, 5 for first class and 5 for economy class. the program picks a random seat for what ever class you choose and if the seat is chosen again, the program should tell you "that seat is taken".
this is my code so far
***************
<html>
<head>
<title>Airline Reservations System</title>
<script type="text/javascript">
<!--
function selectseat()
{
var typeseat = document.myForm.typeseat.value;
var seats = new Array(10)[0,0,0,0,0,0,0,0,0,0];
if (typeseat == 1)
{
var seat = "First Class seat ";
var find = Math.floor(1 + Math.random() * 5);
choice1=find;
//1document.write("find equals " +find);
if (seats[find]!=0)
document.write(""+seat+ " " + choice1+ " is taken<br />");
else
document.write("the seat selected is " + seat + " number " + choice1);
}
else
{
var seat = "Economy seat ";
var choice2 = Math.floor(1 + Math.random() * 5) + 5;
//document.write(""+seat+ " " + choice2+ " is taken<br />");
document.write("the seat selected is " + seat + " number " + choice2);
}
}


//-->
</script>

</head>
<body>
<form name = "myForm" action = "">
Please type 1 for "First Class" <br />
Please type 2 for "Economy"<br />
<input name = "typeseat" type = "text"/><br />
<input name = "calculate" type = "button" value = "Submit" onclick = "selectseat()" />
</form>

</body>
</html>
***********
my problem is that when i read in the seat for first class, i want the seats array to change to 1 for the seat that is taken. When i run this code I get an error at line 24, char 3 which says "undefined is null or not an object"...

Thanks in advance for the help
Johnny

gil davis
03-06-2003, 10:28 AM
Using document.write() like that is going to bite you every time.

When you use document.write() after the page has finished loading, you erase the page. That makes everything "undefined" and "null" and "not an object".

Find another way of displaying your results. Maybe an alert or a text field.

jmassengill
03-06-2003, 10:32 AM
tried using an alert instead of a document.write and got the same results....hmmmm
Thanks for the reply
Johnny

russ801
03-06-2003, 10:41 AM
Won't answer your question directly but have a couple suggestions.

First break it up into two multivatiate arrays. One for first class and one for coach. Since the sets are mutually exclusive, the total number of seats on the plane is irrelevant.

The first element in each array would represent the seat number, the second is if it is filled.

Next, if the seat is selected randomly, the logic should continue until an empty seat of the correct class is found. Does it have to be random or can it be sequential. If, otoh, the seat is selected by the customer, then a message of 'seat filled' is apporopriate.

You may also want to add a function to determine if all the seats in that class are filled.

Russ

jmassengill
03-06-2003, 10:54 AM
from the way i read the directions in the book, the seat must be chosen at random depending on if it is first class or economy. there are a total of 10 seats on the plane and if first class is full a prompt comes up to ask if it is ok to sit in economy class, (and vice versa) if it isn't the program is to print "the next flight leaves in 3 hours"...
I have to use a single script array according to the directions. I have two problems at the moment. 1, I am having a BAD programing slump and 2. I have no idea why the statement " if (seats[find]!=0)" generates an error
Thanks for the input
Johnny

khalidali63
03-06-2003, 10:59 AM
Here you johnny,
You had several mistakes and incomlete code for the functionality you wanted,
Here is the complete working example


<script type="text/javascript">
var ctr=0;
var seats = new Array(0,0,0,0,0,0,0,0,0,0);

function selectseat(){
var typeseat = document.myForm.typeseat.value;
var msg = document.getElementById("message");
if (typeseat == 1){
var seat = "First Class seat ";
var choice1 = Math.floor(1 + Math.random() * 5);
if (validateSeat(choice1)){
msg.innerHTML = (""+seat+ " " + choice1+ " is taken<br />");
}else{
msg.innerHTML = ("the seat selected is " + seat + " number " + choice1);
}
}else{
var seat = "Economy seat ";
var choice2 = Math.floor(1 + Math.random() * 5)+5;
if (validateSeat(choice2)){
msg.innerHTML = (""+seat+ " " + choice2+ " is taken<br />");
}else{
msg.innerHTML = ("the seat selected is " + seat + " number " + choice2);
}

}
}

function validateSeat(num){
var isMatched =false;
for(n=0;n<ctr;n++){
if(seats[n]==num){
isMatched = true;
break;
}
}
if(!isMatched){
seats[ctr] = num;
ctr++;
}
return isMatched;
}
</script>

</head>
<body>
<form name = "myForm" action = "">
Please type 1 for "First Class" <br />
Please type 2 for "Economy"<br />
<input name = "typeseat" type = "text"/><br />
<input name = "calculate" type = "button" value = "Submit" onclick = "selectseat()" />
</form>
<div id="message"></div>
</body>
</html>



Cheers

Khalid

jmassengill
03-06-2003, 11:18 AM
khalidali63...that should go along way towards getting me started.

Thank you
Johnny

jmassengill
03-06-2003, 11:48 AM
from the way i read the directions in the book, the seat must be chosen at random depending on if it is first class or economy. there are a total of 10 seats on the plane and if first class is full a prompt comes up to ask if it is ok to sit in economy class, (and vice versa) if it isn't the program is to print "the next flight leaves in 3 hours"...
I have to use a single script array according to the directions. I have two problems at the moment. 1, I am having a BAD programing slump and 2. I have no idea why the statement " if (seats[find]!=0)" generates an error
Thanks for the input
Johnny

khalidali63
03-06-2003, 11:56 AM
Originally posted by jmassengill
....... I have no idea why the statement " if (seats[find]!=0)" generates an error.....

You have understand the to access a value from the array you need to know the length of the array,so that you do not try to access any value beyond the array index.

The only reasong seats[find]!=0 will generate an error is that find is on t a number that is between 0 and the array.length.

Khalid