gpt4 book ai didi

javascript - 我希望我的javascript忽略数组中的 "1",这样它就会告诉你没有可用空间

转载 作者:行者123 更新时间:2023-12-03 10:57:50 25 4
gpt4 key购买 nike

我正在制作一个座位预订系统,它需要告诉用户是否没有可用的座位,但我似乎无法使循环忽略数字“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")
}
};

最佳答案

首先,我可能不会这样做。与字符串之间的转换太多,等等...但是,如果您继续沿着这条路走,那么我会将问题分解为一些小的函数段,这些函数段将可用座位和不可用座位的计数分开。这将使代码更容易管理和理解。另外,你还有一些我也会避免的全局变量。祝你好运

关于javascript - 我希望我的javascript忽略数组中的 "1",这样它就会告诉你没有可用空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28201504/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com