在javascript中遇到数组和if语句的问题

时间:2015-03-06 22:29:39

标签: javascript arrays

我正在尝试制作一个包含100个门阵列的程序。我做了一个可能不正确的数组。我希望所有100个门都被读为关闭。

为此,我制作了c = closedo = open。我的问题在于if语句。

我希望第一个声明从第二个门开始,然后每2个上升,就像这样

2 4 6 8

等等。每次进门,都需要打开它。然后我需要另外一个if语句,由三个ie 3 6 9 12等等,

当它到达门时,需要关闭打开的门并打开关闭的门。我遇到的问题是如何让if语句查看列表中的每个2和每3个?

    <!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Locker</title>
<script>
    var lockers = [c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c,
                   c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c,
                   c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c,
                   c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c,
                   c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c];
    var c = closed
    var o = open

    function everyTwo(openlockers){
</script>
    </head>
    <body></body>
    </html>

3 个答案:

答案 0 :(得分:0)

一个脚本,可以让您选择每次调用哪个储物柜打开/关闭..更改打开关闭,反之亦然..

<script>
    var c = "closed";
    var o = "open";
    var lockers = [c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c,
        c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c,
        c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c,
        c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c,
        c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c];
    function openLockers(lockerArray,interval) {
        var newArray = lockerArray;
        for (var i = 0; i <= (newArray.length - 1); i++) {
            if ((i+1) % interval === 0) {
                newArray[i] = (newArray[i] == c) ? newArray[i] = o : newArray[i] = c;
            }
        }
        return newArray;
    };
    lockers = openLockers(lockers,2);
</script>

不确定这是不是你想要的。我猜你想要在同一数据上多次迭代运行该函数。

答案 1 :(得分:0)

首先,您应该知道必须先定义变量,然后才能在数组中使用它,因此c数组中的lockers将包含一堆undefined值。这是你可能正在寻找的东西。

function openLockers(lockerCount, start, every){
  var l = lockerCount, a = [];
  for(var i=0; i<l; i++){
    a[i] = false;
  }
  for(var i=start; i<l; i+=every){
    a[i] = true;
  }
  return a;
}
// 100 lockers open every 2 starting at Array element 1, which is the second Array element
var lockers = openLockers(100, 1, 2);
true数组中的

lockers值表示开放式储物柜,而false值表示已关闭的储物柜。请注意,start是一个数组编号。

答案 2 :(得分:0)

JS Fiddle Demo

// Boolean. If true, the door is open, if false, it is closed
var o = false,
    lockers = [o, o, o, o, o, o, o, o, o, o, o, o, o, o, o, o, o, o, o, o,
               o, o, o, o, o, o, o, o, o, o, o, o, o, o, o, o, o, o, o, o,
               o, o, o, o, o, o, o, o, o, o, o, o, o, o, o, o, o, o, o, o,
               o, o, o, o, o, o, o, o, o, o, o, o, o, o, o, o, o, o, o, o,
               o, o, o, o, o, o, o, o, o, o, o, o, o, o, o, o, o, o, o, o],
    count = lockers.length;

function everyN(n){
    for(var i=n-1; i<count; i+=n){
        // reverse the state (open / closed)
        lockers[i] = !lockers[i];
    }
}

everyN(n)功能与您想要的号码一起使用(例如2或3):

// custom function to display the lockers (See in JS Fiddle)
displayLockers('Initial state');
everyN(2);
displayLockers('Every Two');
everyN(2);
displayLockers('Every Two + Every Two = Initial state');
everyN(3);
displayLockers('Every Three');
everyN(2);
displayLockers('Every Three + Every Two');