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

标签 javascript arrays loops

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

相关文章:

javascript - 如何使用类型缺少调用或构造签名的表达式解析 ' error TS2351: Cannot use ' new'?

javascript - 通过 css 和 javascript 的背景图像的 y 位置问题

java - 我的测试方法在运行时返回 null

java - Hadoop:基元数组作为键值对中的值

java - JAVA中单词末尾大写字母后如何反转单词?

c - 增加整数和初始化放置会产生奇怪的效果吗?

javascript - 如何在 HTTP 响应中包含嵌套模型?

javascript - 使用 set() 时,Firebase 不会添加完整的对象

python - Python csv阅读器: loop over csv files

python - 在 Python 中将整数转换为二进制