user2724072 user2724072 - 1 month ago 7
Javascript Question

Invalid left-hand side expression

I'm trying to figure out why the bottom function I've written isn't working properly. Whenever I try to test it out, I get the following error: Uncaught ReferenceError: Invalid left-hand side expression in postfix operation. I'm assuming it's because the code is failing to recognize the "x" string as being the same as the variable x once it's been stripped of it quotation marks when adding to the value of x. Can someone help me finesse this in a way to make it work? If not, can you suggest some alternate approach? Thanks in advance!



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!");
}
}




Answer

The following uses objects. The different grid definitions were tested

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;
        }
    }   
}
Comments