Hi!!
I have a loop on a 2d array and a few if statments inside of it , it’s running will except at the end of the row , one of the if statements i believe is causing the one after it to stop running cause it maight be returning undifned value , i remember seeing someone here using some kind of operator to ignore the undifned values and countinue running the code , if there’s such a thing pleas let me know , or if you have a better idea i would like to hear it .
const pawnsMove = (e, arr, color) => {
classRemover()
for (let i = 0; i < 8; i++) { // enter rows
for (let x = 0; x < 8; x++) { // enter columns
if (arr[i][x] === e.parentNode) {
if (e.classList[2] === 'black') {
if (!arr[i][x].classList[2]) {
if (i === 1) {
arr[i][x]
.classList
.add("marked")
if (!arr[i + 1][x].firstChild) {
//console.log(arr[i+1][x])
arr[i + 1][x]
.classList
.add("marked-empty")
}
if (!arr[i + 2][x].firstChild) {
arr[i + 2][x]
.classList
.add("marked-empty")
}
//console.log(arr[i+1][x+1].classList[2])
if (arr[i + 1][x + 1].firstChild && arr[i + 1][x + 1].firstChild.classList[2] === 'white') {
arr[i + 1][x + 1]
.classList
.add("marked-enemy")
}
if (arr[i + 1][x - 1].firstChild && arr[i + 1][x - 1].firstChild.classList[2] === 'white') {
arr[i + 1][x - 1]
.classList
.add("marked-enemy")
}
}
if (i > 1) {
arr[i][x]
.classList
.add("marked")
if (!arr[i + 1][x].firstChild) {
arr[i + 1][x]
.classList
.add("marked-empty")
}
if (arr[i + 1][x + 1].firstChild && arr[i + 1][x + 1].firstChild.classList[2] === 'white') {
arr[i + 1][x + 1]
.classList
.add("marked-enemy")
}
if (arr[i + 1][x - 1].firstChild && arr[i + 1][x - 1].firstChild.classList[2] === 'white') {
arr[i + 1][x - 1]
.classList
.add("marked-enemy")
}
//arr[i+1][x].classList.add("marked-empty")
}
}
}
// else {
// console.log('on')
// if (!arr[i][x].classList[2]) {
// if (i === 6) {
// arr[i][x]
// .classList
// .add("marked")
// if (!arr[i - 1][x].firstChild) {
// //console.log(arr[i-1][x])
// arr[i - 1][x]
// .classList
// .add("marked-empty")
// }
// //console.log(arr[i-1][x-1].classList[2])
// if (!arr[i - 2][x].firstChild) {
// arr[i - 2][x]
// .classList
// .add("marked-empty")
// }
// if (arr[i - 1][x + 1].firstChild && arr[i - 1][x + 1].firstChild.classList[2] === 'black') {
// arr[i - 1][x + 1]
// .classList
// .add("marked-enemy");
// }
// if (arr[i - 1][x - 1].firstChild && arr[i - 1][x - 1].firstChild.classList[2] === 'black') {
// arr[i - 1][x - 1]
// .classList
// .add("marked-enemy")
// }
// }
// if (i < 6) {
// arr[i][x]
// .classList
// .add("marked")
// if (!arr[i - 1][x].firstChild) {
// arr[i - 1][x]
// .classList
// .add("marked-empty")
// }
// if (arr[i - 1][x - 1].firstChild) {
// if (arr[i - 1][x - 1].firstChild.classList[2] === 'black') {
// arr[i - 1][x - 1]
// .classList
// .add("marked-enemy")
// }
// }
// if (arr[i - 1][x + 1].firstChild) {
// if (arr[i - 1][x + 1].firstChild.classList[2] === 'black') {
// arr[i - 1][x + 1]
// .classList
// .add("marked-enemy")
// }
// }
// //arr[i+1][x].classList.add("marked-empty")
// }
// }
// }
}
}
}
}
the array has 8 arrays in it and each array has 8 elements .
i believe you’ve alredy guessed that im trying to make a chess game here .
You can ignore the commented part since it’s almost the same as the first half .
e = event.target from an event listner