左侧表达式无效

时间:2016-10-14 03:19:05

标签: javascript referenceerror

我正在试图找出为什么我写的底部函数不能正常工作。每当我尝试测试它时,我都会收到以下错误:未捕获的ReferenceError:后缀操作中的左侧表达式无效。我假设它是因为代码在添加到x的值时,一旦它被剥去引号,代码就无法将“x”字符串识别为与变量x相同。有人可以帮我巧妙地使其成功吗?如果没有,你能建议一些替代方法吗?提前致谢!

var grid = [[null, null, null],
				[null, null, null],
				[null, null, null]];

	function checkWin() {

		vals = ["x", "o"];

		var x = 0;
		var o = 0;

		for (var k = 0; k < vals.length; k++) {

			var value = vals[k];

        	for (var i = 0; i < 3; i++) {

            	if (grid[i][i] === value) {

            		(value.replace(/['"]+/g, '')) ++;

            	}

            	if (grid[2 - i][i] === value) {

            		(value.replace(/['"]+/g, '')) ++;

            	}

            	for (var j = 0; j < 3; j++) {

                	if (grid[i][j] === value) {

                		(value.replace(/['"]+/g, '')) ++;

                	}

                	if (grid[j][i] === value) {

                		(value.replace(/['"]+/g, '')) ++;

                	}
            	}
        	}
    	}

    	if (x === 3) {
    		alert("X wins!");
    	} else if (o === 3) {
    		alert("O wins!");
    	}
    }

2 个答案:

答案 0 :(得分:0)

如何将数据放入如下对象:

var _ = {
    x : 0,
    o : 0
};

并改变这样的值:

_[value]++;

我希望这会有所帮助。

答案 1 :(得分:0)

以下使用对象。测试了不同的网格定义

var grid = [
    ["o", "x", "x"],
    ["o", "x", "x"],
    ["o", "", "o"]];

var grid = [
    ["o", "x", ""],
    ["x", "x", "x"],
    ["o", "o", ""]];

var grid = [
    ["x", "x", ""],
    ["o", "x", "o"],
    ["o", "o", "x"]];

var grid = [
    ["x", "x", "o"],
    ["x", "o", "x"],
    ["o", "x", "x"]];

checkWin(grid)      

function checkWin(grid) {

var vals = [{player: "x", value: 0}, {player: "o", value: 0}];

for (var k = 0; k < vals.length; k++) {
    for (var i = 0; i < 3; i++) {
        // check win by row
        vals[k].value = 0;
        // check win by col
        for (var j = 0; j < 3; j++) {
            if (grid[i][j] === vals[k].player) {
                vals[k].value++;
            }
        }
        if (vals[k].value === 3) {
            alert(vals[k].player + " wins by row " + (i+1));
            return vals[k].player;
        }
        // check win by col
        vals[k].value = 0;
        for (var j = 0; j < 3; j++) {
            // check col
            if (grid[j][i] === vals[k].player) {
                vals[k].value++;
                if (vals[k].value === 3) {
                    // break if you want to know what column won by
                    break;
                }
            }
        }
        if (vals[k].value === 3) {
            alert(vals[k].player + " wins by col " + (i+1));
            return vals[k].player;
        }
    }
}
// check win by diag l to r
for (var k = 0; k < vals.length; k++) {
    vals[k].value = 0;
    for (var i = 0; i < 3; i++) {
        if (grid[i][i] === vals[k].player) {
            vals[k].value++;
        }
    }
    if (vals[k].value === 3) {
        alert(vals[k].player + " wins by diag left to right!");
        return vals[k].player;
    }
}   
// check win by diag r to l
for (var k = 0; k < vals.length; k++) {
    vals[k].value = 0;
    for (var i = 0; i < 3; i++) {
        if (grid[i][2-i] === vals[k].player) {
            vals[k].value++;
        }
    }
    if (vals[k].value === 3) {
        alert(vals[k].player + " wins by diag right to left!");
        return vals[k].player;
        }
    }   
}