JavaScript checkerboard

The name of the pictureThe name of the pictureThe name of the pictureClash 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");










share|improve this question















  • 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
















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");










share|improve this question















  • 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












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");










share|improve this question











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");












share|improve this question










share|improve this question




share|improve this question









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












  • 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










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.






share|improve this answer




























    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];









    share|improve this answer





















      Your Answer




      StackExchange.ifUsing("editor", function ()
      return StackExchange.using("mathjaxEditing", function ()
      StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
      StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
      );
      );
      , "mathjax-editing");

      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "196"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      convertImagesToLinks: false,
      noModals: false,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );








       

      draft saved


      draft discarded


















      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






























      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.






      share|improve this answer

























        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.






        share|improve this answer























          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.






          share|improve this answer













          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.







          share|improve this answer













          share|improve this answer



          share|improve this answer











          answered Mar 23 at 1:12









          Blindman67

          5,3611320




          5,3611320






















              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];









              share|improve this answer

























                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];









                share|improve this answer























                  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];









                  share|improve this answer













                  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];










                  share|improve this answer













                  share|improve this answer



                  share|improve this answer











                  answered Mar 23 at 0:47









                  Marc Rohloff

                  2,57735




                  2,57735






















                       

                      draft saved


                      draft discarded


























                       


                      draft saved


                      draft discarded














                      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













































































                      Popular posts from this blog

                      Greedy Best First Search implementation in Rust

                      Function to Return a JSON Like Objects Using VBA Collections and Arrays

                      C++11 CLH Lock Implementation