switch / if语句打破for循环

时间:2015-12-07 02:06:31

标签: javascript

到目前为止,我花了3个小时试图修复for循环。我已经尝试了一切......如果我删除switch / if语句,for循环工作正常。请指教。

背景:This is an exercise where a grid of 1000x1000 lights has to be turned on/off by following a set of instructions with coordinates.

此代码没问题。我在这里声明了对象和函数。

var data = ["toggle 461,550 through 564,900", "turn off 370,39 through 425,839", "turn on 599,989 through 806,993"];

var instructions = []; //301

for(i = 0; i < data.length; i++){
var instruction = data[i].match(/\d+/g);
    if(data[i].match(/on/)) instruction.splice(0, 0, "on")
    else if(data[i].match(/off/)) instruction.splice(0, 0, "off")
    else if(data[i].match(/toggle/)) instruction.splice(0, 0, "toggle")

    instructions.push(instruction);
    console.log("Instructions populated.");
}

function Light(x, y, s) { // Constructor
    this.x = x;
    this.y = y;
    this.s = s;
}

var lights = []; // The grid

for(x = 0; x < 10; x++) {// Populates the grid
    for(y = 0; y < 10; y++) {
        lights.push(new Light(x, y, false));
    }
    console.log("Grid populated.");
}

function turnOn(x1, y1, x2, y2) {
    for (i = 0; i < lights.length; i++) {
        if(lights[i].x >= x1 && lights[i].x <= x2 
        && lights[i].y >= y1 && lights[i].y <= y2) {
            lights[i].s = true;
        }
        //console.log(lights[i]);
    }
    console.log("Turning on DONE");
}

function turnOff(x1, y1, x2, y2) {
    for (i = 0; i < lights.length; i++) {
        if(lights[i].x >= x1 && lights[i].x <= x2 
        && lights[i].y >= y1 && lights[i].y <= y2) {
            lights[i].s = false;
        }
    }
    console.log("Turning off DONE");
}

function toggle(x1, y1, x2, y2) {
    for (i = 0; i < lights.length; i++) {
        if(lights[i].x >= x1 && lights[i].x <= x2 
        && lights[i].y >= y1 && lights[i].y <= y2) {
            lights[i].s = !lights[i].s;
        }
    }
    console.log("Toggling DONE");
}

这是有问题的部分。我不知道它为什么不能工作。

console.log("For Loop start");
for(i = 0; i < instructions.length; i++){

    var action = instructions[i][0];
    var x1 = instructions[i][1];
    var y1 = instructions[i][2];
    var x2 = instructions[i][3];
    var y2 = instructions[i][4];
    console.log(action, x1, y1, x2, y2);

    switch(action){ // This breaks the loop.
        case "on": 
            turnOn(x1, y1, x2, y2); 
            break;
        case "off": 
            turnOff(x1, y1, x2, y2); 
            break;
        case "toggle": 
            toggle(x1, y1, x2, y2); 
            break;
    }
}

输出

填充说明。
网格填充。
对于循环启动
拨动461 550 564 900
切换完成

为什么其他两条说明不会被解雇?

2 个答案:

答案 0 :(得分:1)

function toggle(x1, y1, x2, y2) {
    for (i = 0; i < lights.length; i++) {

toggle重复使用i,因为此函数中没有var i,所以它使用外部作用域中的i。即你的循环。

答案 1 :(得分:0)

您在for循环中使用全局变量Enemy(float ObjectX, float ObjectY, float ObjectVX, float ObjectVY, char* Name, float ObjectD, int Onground, bool flipflag, bool visflag, int Health, int Agility, int Mana, int LootTable[3], int ProbTable[3]); 并在i函数中使用相同的变量,因此循环结束。

将所有for循环更改为:

toggle()