JavaScript checkerboard
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
-1
down vote
favorite
Can anyone take a look at this Javascript code and see if there is an opportunity for simplifying. The code runs and generates a chess board by adding a dark color class or light to each cell in the board.
function colours()
var rows = document.querySelectorAll(".row");
for (var i = 1; i <= 8; i++)
var currRow = rows[i];
var cells = currRow.querySelectorAll(".cell");
if(i%2==0)
for (var j = 1; j < cells.length; j++)
var currCell = cells[j];
if(j%2==0)
currCell.classList.add("dark");
else
currCell.classList.add("light");
else
for (var j = 1; j < cells.length; j++)
var currCell = cells[j];
if(j%2==0)
currCell.classList.add("light");
else
currCell.classList.add("dark");
javascript
add a comment |Â
up vote
-1
down vote
favorite
Can anyone take a look at this Javascript code and see if there is an opportunity for simplifying. The code runs and generates a chess board by adding a dark color class or light to each cell in the board.
function colours()
var rows = document.querySelectorAll(".row");
for (var i = 1; i <= 8; i++)
var currRow = rows[i];
var cells = currRow.querySelectorAll(".cell");
if(i%2==0)
for (var j = 1; j < cells.length; j++)
var currCell = cells[j];
if(j%2==0)
currCell.classList.add("dark");
else
currCell.classList.add("light");
else
for (var j = 1; j < cells.length; j++)
var currCell = cells[j];
if(j%2==0)
currCell.classList.add("light");
else
currCell.classList.add("dark");
javascript
1
Can you provide the html and css you are using ? Possibly setting up a live snippet on the question.
â Isac
Mar 22 at 23:23
add a comment |Â
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
Can anyone take a look at this Javascript code and see if there is an opportunity for simplifying. The code runs and generates a chess board by adding a dark color class or light to each cell in the board.
function colours()
var rows = document.querySelectorAll(".row");
for (var i = 1; i <= 8; i++)
var currRow = rows[i];
var cells = currRow.querySelectorAll(".cell");
if(i%2==0)
for (var j = 1; j < cells.length; j++)
var currCell = cells[j];
if(j%2==0)
currCell.classList.add("dark");
else
currCell.classList.add("light");
else
for (var j = 1; j < cells.length; j++)
var currCell = cells[j];
if(j%2==0)
currCell.classList.add("light");
else
currCell.classList.add("dark");
javascript
Can anyone take a look at this Javascript code and see if there is an opportunity for simplifying. The code runs and generates a chess board by adding a dark color class or light to each cell in the board.
function colours()
var rows = document.querySelectorAll(".row");
for (var i = 1; i <= 8; i++)
var currRow = rows[i];
var cells = currRow.querySelectorAll(".cell");
if(i%2==0)
for (var j = 1; j < cells.length; j++)
var currCell = cells[j];
if(j%2==0)
currCell.classList.add("dark");
else
currCell.classList.add("light");
else
for (var j = 1; j < cells.length; j++)
var currCell = cells[j];
if(j%2==0)
currCell.classList.add("light");
else
currCell.classList.add("dark");
javascript
asked Mar 22 at 19:25
RobinQ
1
1
1
Can you provide the html and css you are using ? Possibly setting up a live snippet on the question.
â Isac
Mar 22 at 23:23
add a comment |Â
1
Can you provide the html and css you are using ? Possibly setting up a live snippet on the question.
â Isac
Mar 22 at 23:23
1
1
Can you provide the html and css you are using ? Possibly setting up a live snippet on the question.
â Isac
Mar 22 at 23:23
Can you provide the html and css you are using ? Possibly setting up a live snippet on the question.
â Isac
Mar 22 at 23:23
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
1
down vote
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.
add a comment |Â
up vote
0
down vote
You can reduce the complex if statement and the resulting duplication by checking if i+j
is even or odd to color a place. i.e.
function colours()
var rows = document.querySelectorAll(".row");
for (var i = 1; i <= 8; i++)
var currRow = rows[i];
var cells = currRow.querySelectorAll(".cell");
for (var j = 1; j <= 8; j++)
var color = (i+j) % 2==0 ? "light" : "dark";
currCell.classList.add("dark");
var currCell = cells[j];
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
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.
add a comment |Â
up vote
1
down vote
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.
add a comment |Â
up vote
1
down vote
up vote
1
down vote
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.
answered Mar 23 at 1:12
Blindman67
5,3611320
5,3611320
add a comment |Â
add a comment |Â
up vote
0
down vote
You can reduce the complex if statement and the resulting duplication by checking if i+j
is even or odd to color a place. i.e.
function colours()
var rows = document.querySelectorAll(".row");
for (var i = 1; i <= 8; i++)
var currRow = rows[i];
var cells = currRow.querySelectorAll(".cell");
for (var j = 1; j <= 8; j++)
var color = (i+j) % 2==0 ? "light" : "dark";
currCell.classList.add("dark");
var currCell = cells[j];
add a comment |Â
up vote
0
down vote
You can reduce the complex if statement and the resulting duplication by checking if i+j
is even or odd to color a place. i.e.
function colours()
var rows = document.querySelectorAll(".row");
for (var i = 1; i <= 8; i++)
var currRow = rows[i];
var cells = currRow.querySelectorAll(".cell");
for (var j = 1; j <= 8; j++)
var color = (i+j) % 2==0 ? "light" : "dark";
currCell.classList.add("dark");
var currCell = cells[j];
add a comment |Â
up vote
0
down vote
up vote
0
down vote
You can reduce the complex if statement and the resulting duplication by checking if i+j
is even or odd to color a place. i.e.
function colours()
var rows = document.querySelectorAll(".row");
for (var i = 1; i <= 8; i++)
var currRow = rows[i];
var cells = currRow.querySelectorAll(".cell");
for (var j = 1; j <= 8; j++)
var color = (i+j) % 2==0 ? "light" : "dark";
currCell.classList.add("dark");
var currCell = cells[j];
You can reduce the complex if statement and the resulting duplication by checking if i+j
is even or odd to color a place. i.e.
function colours()
var rows = document.querySelectorAll(".row");
for (var i = 1; i <= 8; i++)
var currRow = rows[i];
var cells = currRow.querySelectorAll(".cell");
for (var j = 1; j <= 8; j++)
var color = (i+j) % 2==0 ? "light" : "dark";
currCell.classList.add("dark");
var currCell = cells[j];
answered Mar 23 at 0:47
Marc Rohloff
2,57735
2,57735
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f190233%2fjavascript-checkerboard%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
Can you provide the html and css you are using ? Possibly setting up a live snippet on the question.
â Isac
Mar 22 at 23:23