i am making a seat booking system and it needs to tell the user if there are no seats available, but i cannot seem to make the loop ignore the number "1" which means it will not tell the user if there are no seats available it just says the last seat set to "0" and the first seat they booked. any help would be great.
<script>
function readFile()
{
var file = [];
//seat configuration is set up in a javascript array
file[0] = "A,1,1,1,1,1,1,1,0,0,0";
file[1] = "B,0,0,0,0,0,0,0,0,0,0";
file[2] = "C,0,0,0,0,0,0,0,0,0,0";
file[3] = "D,0,0,0,0,0,0,0,0,0,0";
file[4] = "E,0,0,0,0,0,0,0,0,0,0";
return file;
};
function lookForSeats(row, wantedSeats)
{
var done = true;
var firstSeat = 0;
var bookedSeats = 0;
//this loops through the array looking for the selected row
for (var x = 1; x < row.length; x++)
{
if (row[x] == 0)
{
row[x] = 0;
//this if statement then remebers the first seat booked
if (firstSeat == 0)
{
firstSeat = x;
}
//it then cycles again from the first seat and looks for the other seats requested
bookedSeats = bookedSeats + 1;
if (bookedSeats == wantedSeats)
{
alert("Your seats have be located");
break;
}
}
}
//this is just the alert to show the seats available at the end
if (firstSeat != 0)
{
var lastSeat = wantedSeats;
alert("Seats " + row[0] + firstSeat + " - " + row[0] + lastSeat + " are available.");
}
return done;
};
//this whole function is just to pull the information entered by the user into the loop and to split the array up
function processBooking()
{
var success = false;
var row = document.getElementById("rowField");
var numSeats = document.getElementById("seatsField");
var layout = readFile();
for (var x = 0; x < layout.length; x++)
{
//this is the part that splits the array up into seperate strings. for example: "0,0,0,0" would become "0" "0" "0" "0"
thisRow = layout[x].split(',');
if (row.value == thisRow[0])
{
//this is to stop the loop once it has found the row you want
success = lookForSeats(thisRow, numSeats.value);
break;
}
}
if (success == true)
{
}
else
{
alert("seats are not available")
}
};
Aucun commentaire:
Enregistrer un commentaire