'Why wont the value change in the array?
why is it console.logging the array with all zeroes??
im clearly changing value but its still console.logging with just zeroes
let a = 0,
b = 0,
c = 0,
d = 0,
e = 0
f = 0,
g = 0,
h = 0,
i = 0;
const test = document.querySelector(".test");
test.addEventListener("click", () => {
a = 1;
b = 1;
c = 1;
gameBoard.winConditions;
console.log(gameBoard.winConditions)
})
const gameBoard = {
winConditions: [[a, b, c], [a, d, g], [a, e, i], [b ,e, h], [c, e, g],
[c, f, i], [d, e, f], [g, h, i]]
}
Solution 1:[1]
gameBoard.winConditions; this line does nothing. When updating the a,b,c... variables, the winConditions stay the same. I would recommend to write a function to re-evaluate the value of winConditions:
let a = 0,
b = 0,
c = 0,
d = 0,
e = 0
f = 0,
g = 0,
h = 0,
i = 0;
const test = document.querySelector(".test");
test.addEventListener("click", () => {
a = 1;
b = 1;
c = 1;
applyWinConditions();
console.log(gameBoard.winConditions)
})
const gameBoard = {};
applyWinConditions(); // set the win conditions initially
function applyWinConditions() {
gameBoard.winConditions = [[a, b, c], [a, d, g], [a, e, i], [b ,e, h], [c, e, g], [c, f, i], [d, e, f], [g, h, i]];
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | JSON Derulo |
