我从一个教程中得到了GOL simulator分.当我在VSCode的本地主机端口上运行它,并在我的浏览器(Chrome)上转到那个URL时,它不起作用.我认为这是代码中的错误,所以我将所有常量函数(const a = function(){})更改为常规表示法(function a(){}).但这并不管用.我试着进入JSBin,并把我的HTML、CSS和JS放进go .奇迹般地,它成功了!

"use strict";

const rows = 40;
const cols = 40;

let currGen = [rows];
let nextGen = [rows];



function createGenArrays() {
    for (let i = 0; i < rows; i++) {
        currGen[i] = new Array(cols);
        nextGen[i] = new Array(cols);
    }
}



function initGenArrays() {
    for (let i = 0; i < rows; i++) {
        for (let j = 0; j < cols; j++) {
            currGen[i][j] = 0;
            nextGen[i][j] = 0;
        }
    }
}



function createWorld() {
    let world = document.querySelector('#world');
    let table = document.createElement('table');
    table.setAttribute('id', 'worldgrid');
    for (let i = 0; i < rows; i++) {
        let tr = document.createElement('tr');
        for (let j = 0; j < cols; j++) {
            let cell = document.createElement('td');
            cell.setAttribute('id', i + '_' + j);
            cell.setAttribute('class', 'dead');
            cell.addEventListener('click', cellClick);
            tr.appendChild(cell);
        }
        table.appendChild(tr);
    }
    world.appendChild(table)
}



function cellClick() {
    let loc = this.id.split('_');
    let row = Number(loc[0]); // Get i
    let col = Number(loc[1]); // Get j

    if (this.className === 'alive') { // Toggle cell state
        this.setAttribute('class', 'dead');
        currGen[row][col] = 0;
    }
    else {
        this.setAttribute('class', 'alive');
        currGen[row][col] = 1;
    }
}



function createNextGen() {
    for (row in currGen) {
        for (col in currGen[row]) { // Check `rules.md`
            let neighbors = getNeighborCount(row, col);

            if (currGen[row][col] == 1) { // If alive
                if (neighbors < 2) nextGen[row][col] = 0;
                else if (neighbors == 2 || neighbors == 3) nextGen[row][col] = 1;
                else if (neighbors > 3) nextGen[row][col] = 0;
            }
            else if (currGen[row][col] == 0) { // If dead
                if (neighbors == 3) {
                    nextGen[row][col] = 1; // you spin me right round baby right round
                } // like a record baby right round round round baby this is propogation
            }
        }
    }
}



function updateCurrGen() {
    for (row in currGen) {
        for (col in currGen[row]) { // left shift
            currGen[row][col] = nextGen[row][col];
            nextGen[row][col] = 0;
        }
    }
}



function updateWorld() {
    let cell = '';
    for (row in currGen) {
        for (col in currGen[row]) {
            cell = document.getElementById(row + '_' + col);
            if (currGen[row][col] == 0) cell.setAttribute('class', 'dead');
            else cell.setAttribute('class', 'alive');
        }
    }
}



function evolve() {
    createNextGen();
    updateCurrGen();
    updateWorld();
}



function getNeighborCount(row, col) {
    let count = 0;
    let nrow = Number(row);
    let ncol = Number(col);

    if (nrow - 1 >= 0) { // Make sure we aren't at first row
        if (currGen[nrow - 1][ncol] == 1) count++; // Check north neighbor
    }

    if (nrow - 1 >= 0 && ncol - 1 >= 0) { // Make sure we aren't at first row or col
        if (currGen[nrow - 1][ncol - 1] == 1) count++; // Check northwest neighbor
    }

    if (nrow - 1 >= 0 && ncol + 1 < cols) { // Make sure we aren't at first row or last col
        if (currGen[nrow - 1][ncol + 1] == 1) count++; // Check northeast neighbor
    }

    if (ncol - 1 >= 0) { // Make sure we aren't at first col
        if (currGen[nrow][ncol - 1] == 1) count++; // Check west neighbor
    }

    if (ncol + 1 < cols) { // Make sure we aren't at last col
        if (currGen[nrow][ncol + 1] == 1) count++; // Check east neighbor
    }

    if (nrow + 1 < rows && ncol - 1 >= 0) { // Make sure we aren't at last row or first col
        if (currGen[nrow + 1][ncol - 1] == 1) count++; // Check southwest neighbor
    }

    if (nrow + 1 < rows && ncol + 1 < cols) { // Make sure we aren't at last row or col
        if (currGen[nrow + 1][ncol + 1] == 1) count++; // Check southeast neighbor
    }

    if (nrow + 1 < rows) { // Make sure we aren't at last row
        if (currGen[nrow + 1][ncol] == 1) count++; // Check south neighbor
    }

    return count;
}



window.onload = () => {
    createWorld(); // Visual table
    createGenArrays(); // Current and next gens
    initGenArrays(); // Initialize all to dead
    //window.alert('success');
}
#world {
    padding-bottom: 10px;
}

table {
    background-color: whitesmoke;
}

td {
    border: 1px solid darkgray;
    width: 10px;
    height: 10px;
}
td.dead {
    background-color: transparent;
}
td.alive {
    background-color: blue;
}
<!DOCTYPE html>

<html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>Game Of Life</title>
        <!--<link rel="stylesheet" type="text/css" href="style.css" /> commented out for sharing on SO -->
    </head>
    <body>
        <div id="world"></div>
        <div>
            <input type="button" value="Evolve" onclick="evolve()".>
        </div>
        <!--<script src="script.js"></script>-->
    </body>
</html>

我真的很困惑这一点,我非常确定是我的代码有问题,比如可能使用在 node 上工作的过时的东西.因为它是一个前端应用程序,所以在本地运行它应该有no个问题(我也try 了直接运行HTML文件,而不是使用本地主机).

我没有配备Node,所以我不能试.但我在下面附上了我的文件,我希望你能修复它.

运行代码,这样您才能理解我的问题!我的问题是,当我点击"演进"按钮时,什么也没有发生.它应该传给下一代,但它只是原地踏步.

编辑:当我在so上运行这个程序时,它在控制台中给出了一个错误.很奇怪,因为我在本地或在JSBin上运行它时没有收到这个错误.

推荐答案

通常,控制台中的错误不会说谎.一个来自你的片段说"行未定义".这可能不是唯一的一个错误,但至少在这种情况下(以及类似情况下),

  for (row in currGen)
    for (col in currGen[row])

应该是

  for (let row in currGen)
    for (let col in currGen[row])

我想知道为什么它did能在某个地方工作.可能是严格模式被意外忽略了.

Javascript相关问答推荐

获取表格的左滚动位置

为什么getRecord()会因为与_logger相关的错误而失败?(使用Hedera SDK)

可以的.是否可以在不预编译的情况下使用嵌套 Select 器?

为什么从liveWire info js代码传递数组我出现错误?

Javascript,部分重排序数组

Regex结果包含额外的match/group,只带一个返回

无法读取未定义错误的属性路径名''

当id匹配时对属性值求和并使用JavaScript返回结果

如何发送从REST Api收到的PNG数据响应

第三方包不需要NODE_MODULES文件夹就可以工作吗?

邮箱密码重置链接不适用于已部署的React应用程序

如何在HTMX提示符中设置默认值?

在查看网页时,如何使HTML中的按钮工作方式类似于鼠标上的滚轮或箭头键?

为什么在运行于<;img>;事件处理程序中的JavaScript中x和y是不可变的?

如何在TransformControls模式下只保留箭头进行翻译?

调用特定数组索引时,为什么类型脚本不判断未定义

Plotly.js栏为x轴栏添加辅助文本

如何阻止外部脚本进入顶层导航

我想为我的Reaction项目在画布上加载图像/视频,图像正在工作,但视频没有

Google OAuth 2.0库和跨域开放程序的问题-策略错误