用不同的逻辑替换我的continue语句

时间:2018-10-14 20:47:15

标签: javascript eslint

    do {
        const randX = randomNumberBetween(0, 9);
        const randY = randomNumberBetween(0, 9);
        if ((randX === x1 && randY === y1) || (randX === x2 && randY === y2)) {
            continue;
        }
        matrix[randX][randY] = 'Obstacle';
        placedBlocks += 1;
    } while (placedBlocks < blocks);
    matrix[x1][y1] = 'Start';
    matrix[x2][y2] = 'Goal';
    return matrix;
}

我需要用其他逻辑替换我的continue语句,因为我的ESLint规则不允许我使用它。

我的整个任务是通过带有块字段的矩阵创建寻路算法,并且一切正常。我只需要用其他东西代替continue;

2 个答案:

答案 0 :(得分:2)

不是跳过if条件下的循环,只需反转if条件并在if条件下进行工作,

do {
    const randX = randomNumberBetween(0, 9);
    const randY = randomNumberBetween(0, 9);
    if (!((randX === x1 && randY === y1) || (randX === x2 && randY === y2))) {
        matrix[randX][randY] = 'Obstacle';
        placedBlocks += 1;
    }
} while (placedBlocks < blocks);
    matrix[x1][y1] = 'Start';
    matrix[x2][y2] = 'Goal';
    return matrix;
}

答案 1 :(得分:0)

您的条件语句当前检查randXrandY坐标对是否与两个点之一重叠。您可以反转代码中的逻辑,在continue内用if移动要跳过的块。

反演是检查randXrandY是否不重叠(emem)重叠两个点之一,方法是否定先前的有条件条件,或者更干净一点地翻转{{ 1}}至===!==&&

||

这是逻辑对等的有效演示:

do {
    const randX = randomNumberBetween(0, 9);
    const randY = randomNumberBetween(0, 9);

    if ((randX !== x1 || randY !== y1) && (randX !== x2 || randY !== y2)) {
        matrix[randX][randY] = 'Obstacle';
        placedBlocks += 1;
    }
} while (placedBlocks < blocks);

matrix[x1][y1] = 'Start';
matrix[x2][y2] = 'Goal';
return matrix;