##Zero indexed array and clean code.
Zero indexed array and clean code.
The for
loops are a little strange starting at 1. Normally you index into an array from 0. If you have 8 items in the array the loop would be for(i = 0; i < array.length; i ++){
but you may have extra elements on the array. Without the HTML I can only guess.
For finding the checker colour you can add the x and y (i + j) % 2
and then get the remainder of 2 saving you the second loop.
Function names should tell you what they do. You have colours
which could mean anything.
The whole thing can be done as follows
function createCheckerBoard() {
const rows = document.querySelectorAll(".row");
for (let i = 0; i < 8; i++) {
var cells = rows[i].querySelectorAll(".cell");
for (let j = 0; j < cells.length; j++) {
cells[j].classList.add((j + i) % 2 ? "light": "dark");
}
}
}
Using const
for variables that do not change and a ternary operator for the class selection.
The most important part of coding is good and consistent style. Having messy code makes it harder to read and near impossible sometimes to spot subtle syntax bugs. Many IDE's can format the code for you, learn to use them they will save you many hours of bug hunting frustration.
##Zero indexed array and clean code.
The for
loops are a little strange starting at 1. Normally you index into an array from 0. If you have 8 items in the array the loop would be for(i = 0; i < array.length; i ++){
but you may have extra elements on the array. Without the HTML I can only guess.
For finding the checker colour you can add the x and y (i + j) % 2
and then get the remainder of 2 saving you the second loop.
Function names should tell you what they do. You have colours
which could mean anything.
The whole thing can be done as follows
function createCheckerBoard() {
const rows = document.querySelectorAll(".row");
for (let i = 0; i < 8; i++) {
var cells = rows[i].querySelectorAll(".cell");
for (let j = 0; j < cells.length; j++) {
cells[j].classList.add((j + i) % 2 ? "light": "dark");
}
}
}
Using const
for variables that do not change and a ternary operator for the class selection.
The most important part of coding is good and consistent style. Having messy code makes it harder to read and near impossible sometimes to spot subtle syntax bugs. Many IDE's can format the code for you, learn to use them they will save you many hours of bug hunting frustration.
Zero indexed array and clean code.
The for
loops are a little strange starting at 1. Normally you index into an array from 0. If you have 8 items in the array the loop would be for(i = 0; i < array.length; i ++){
but you may have extra elements on the array. Without the HTML I can only guess.
For finding the checker colour you can add the x and y (i + j) % 2
and then get the remainder of 2 saving you the second loop.
Function names should tell you what they do. You have colours
which could mean anything.
The whole thing can be done as follows
function createCheckerBoard() {
const rows = document.querySelectorAll(".row");
for (let i = 0; i < 8; i++) {
var cells = rows[i].querySelectorAll(".cell");
for (let j = 0; j < cells.length; j++) {
cells[j].classList.add((j + i) % 2 ? "light": "dark");
}
}
}
Using const
for variables that do not change and a ternary operator for the class selection.
The most important part of coding is good and consistent style. Having messy code makes it harder to read and near impossible sometimes to spot subtle syntax bugs. Many IDE's can format the code for you, learn to use them they will save you many hours of bug hunting frustration.
##Zero indexed array and clean code.
The for
loops are a little strange starting at 1. Normally you index into an array from 0. If you have 8 items in the array the loop would be for(i = 0; i < array.length; i ++){
but you may have extra elements on the array. Without the HTML I can only guess.
For finding the checker colour you can add the x and y (i + j) % 2
and then get the remainder of 2 saving you the second loop.
Function names should tell you what they do. You have colours
which could mean anything.
The whole thing can be done as follows
function createCheckerBoard() {
const rows = document.querySelectorAll(".row");
for (let i = 0; i < 8; i++) {
var cells = rows[i].querySelectorAll(".cell");
for (let j = 0; j < cells.length; j++) {
cells[j].classList.add((j + i) % 2 ? "light": "dark");
}
}
}
Using const
for variables that do not change and a ternary operator for the class selection.
The most important part of coding is good and consistent style. Having messy code makes it harder to read and near impossible sometimes to spot subtle syntax bugs. Many IDE's can format the code for you, learn to use them they will save you many hours of bug hunting frustration.