[英]I want my javascript to ignore “1” in the array so that it will tell you that there is no space available
我正在制作一個座位預定系統,它需要告訴用戶是否沒有可用的座位,但是我似乎無法使循環忽略數字“ 1”,這意味着它不會告訴用戶是否沒有可用的座位只是說最后一個座位設置為“ 0”,他們預定的第一個座位。 任何幫助都會很棒。
<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")
}
};
首先,我可能不會這樣做。 到/從字符串的轉換太多,等等。但是,如果您堅持這一步,那么我會將問題分解為一些小的功能部分,這些部分將可用座位數和不可用座位數分開。 這將使代碼更易於管理和理解。 另外,您也有一些全局變量,我也要避免。 祝好運
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.