I am trying to assign the value of boxSpaces[0]
to the variable result in the function decalreWinner()
but the assignment is not taking place.
function assignSpace(mark, box) {
const image = document.createElement("img");
const src = mark === "x" ? X_IMAGE_URL : O_IMAGE_URL;
image.src = src;
box.appendChild(image);
box.removeEventListener("click", changeToX);
const id = parseInt(box.dataset.id);
boxSpaces[id] = mark;
}
function isGameOver() {
const length = emptyBoxes.length;
if (length <= 0) {
declareWinner();
return true;
} else {
false;
}
}
function declareWinner() {
const result = "";
if (boxSpaces["0"] === boxSpaces["1"] && boxSpaces["1"] === boxSpaces["2"]) {
result = boxSpaces["0"];
console.log(result);
}
return result;
}
You are using const
to declare a variable and you are tring to update it which is not possible. change it to let
or var
function declareWinner() {
let result = ""; //change it to let or var
if (boxSpaces["0"] === boxSpaces["1"] && boxSpaces["1"] === boxSpaces["2"]) {
result = boxSpaces["0"];
console.log(result);
}
return result;
}