简体   繁体   English

简单的数组和条件javascript

[英]simple arrays and conditions javascript

So im a newbie and I need some basic help. 所以我是新手,我需要一些基本帮助。 I wanna make a program that checks whether room is free or taken. 我想制作一个检查房间是否空闲或占用的程序。 I dont know if i can make just a variable with all free rooms or should i do this the other way. 我不知道我是否可以使所有免费房间成为一个变量,或者我应该以其他方式这样做。 I'm kinda stuck on solving the problem. 我有点想解决问题。

function action() {
    var room = document.getElementById('text');
    var free = [1, 2, 3];
    if (room.value == free) {
        alert('Hello');
    } else {
        alert('Taken');
    }
}

document.getElementById('button').onclick = function() {
    action();
}

I wanna ask if is it possible to just compare variable i enter to other variable with list of free rooms if not, how to make this work then? 我想问是否可以将输入的变量与其他变量进行比较,如果没有,那么如何使这项工作有效?

You can't just compare your number with an array. 您不能只是将您的数字与数组进行比较。 You need to check if your number is in the array: 您需要检查您的号码是否在数组中:

if (free.indexOf(room.value) > -1)

Also, assuming you're using a text field, you'll need to convert room.value to a Number (instead of String) before checking: 另外,假设您使用的是文本字段,则需要在检查之前将room.value转换为数字(而不是字符串):

if (free.indexOf(+room.value) > -1)

You can learn more about indexOf() on MDN. 您可以了解有关MDN的indexOf()更多信息。

The line if (room.value == free) compares a string (the value) with an array . if (room.value == free) 字符串 (值)与array比较 That won't usually be true. 那通常是不正确的。

To find a value in an array, you use Array#indexOf . 要在数组中查找值,请使用Array#indexOf It does strict comparison, so since your values in the array are numbers, you have to make sure that your value is a number as well. 它进行严格的比较,因此,由于数组中的值是数字,因此必须确保您的值也是数字。

So 所以

if (free.indexOf(+room.value) != -1) {
    // Yes it's there
}

The unary + is one way to convert strings to numbers. 一元+是将字符串转换为数字的一种方法。 Another is parseInt , which lets you specify a radix (number base): 另一个是parseInt ,它允许您指定一个基数(数字基):

if (free.indexOf(parseInt(room.value, 10)) != -1) {
    // Yes it's there
}

You have other options as well; 您还有其他选择。 this answer lists the various options and the pros and cons of them. 该答案列出了各种选择以及它们的优缺点。

You can try this. 你可以试试看 Going further, you can remove the room number taken from the list of free rooms and push them into list of rooms already taken. 更进一步,您可以从空闲房间列表中删除已占用的房间号,并将其推入已占用的房间列表中。

 function action() { var isRoomAvailable = false; var room = document.getElementById('text').value; var freeRoom = [1, 2, 3]; // 4-10 are taken rooms. var takenRooms = [4, 5, 6, 7, 8, 9, 10]; for (var i = 0; i < freeRoom.length; i++) { if (room == freeRoom[i]) { alert('Hello, You can take room '+room); isRoomAvailable = true; return; } } if (!isRoomAvailable) alert("Sorry, Room number " + room + " is taken"); } document.getElementById('button').onclick = function() { action() } 
 Enter Room number(1-10): <input type="text" id="text" /> <button id="button">Search Room</button> 

You should perhaps use a 0 and 1 system for storing whether a room is taken. 您也许应该使用0和1系统来存储是否占用了房间。 So 0 is for taken and 1 is for free or vice versa depending on your liking. 因此,根据您的喜好,0是带走的,1是免费的,反之亦然。 So all it would have to do is test for whether in the array, the room's value is 0 or 1 因此,要做的就是测试数组中房间的值是0还是1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM