Make a dynamic chessboard responsive to jQuery user input

I am working on a coding task, proposing to create a chessboard using jQuery, as well as an input form requesting how big the game board is (for example, a 6x6 board, a 7x7 board, etc.). I am having difficulty with the odd / even class assignment when the input is an odd number (i.e. 7x7 board, 9x9 board, 11x11 board). If you run the JS script while increasing # input by any odd number, the odd / even assignment skips.

$(document).ready(function() {
    
  //create input form for number/size of board
  $('h1').append('<div class=div1>Size of Board: <input type="text" id = "size1" name="size2" min = "2" max = "100" step = "2" value = "6"><input type="submit" id="submit1" value="Create"></div>')
 
  //create button to print Game Pieces 
  $('h1').append('<div><input type="submit" id="submitForm" value="Lets Play!"></div>');
  
  
  var z = '';
  //on clicking the button, create array of empty boxes/<td>
  $('#submit1').click(function() {
    var array = [];
    //remove previous appended array
    $('tbody').empty();

    //grab current value or size of gameboard
    z = $('#size1').val();
    
      //with a for loop, create "empty" table (z by z) of boxes
      for (var i=0; i<z; i++) {
        //addClass so we can grab later for color assignment
        var trEven = $('<tr>').addClass('trEven');
        var trOdd = $('<tr>').addClass('trOdd');
        //Differentiate between row to row: assign class trEven and trOdd to every other row      
        if (i%2 == 0) {
          array.push(trEven);
        }
        else {
          array.push(trOdd);
        }
        //for each row add z number of td's
          for (var j=0; j<z; j++) {
            array[i].append('<td></td>');
          }
      } 

    //append updated array to <tbody>
    $('tbody').append(array);
    //select all evens/odds of trOdd/trEven to assign colors using special selectors
    $('.trOdd td:odd').css('background-color', 'black'); 
    $('.trOdd td:even').css('background-color', 'white'); 
    $('.trEven td:odd').css('background-color', 'white');
    $('.trEven td:even').css('background-color', 'black'); 

  });//onclick function
  
  
  //Play Button: add game pieces
  $('#submitForm').click(function(){
    if (z <= 6) {
      //first two rows
      $('#gameBoard tr:eq(0) td:odd').append('<div class="gamePiece">')
      $('#gameBoard tr:eq(1) td:even').append('<div class="gamePiece">')
      //last two rows
      $('#gameBoard tr:last td:even').append('<div class="gamePiece">')
      $('#gameBoard tr:nth-last-child(2) td:odd').append('<div class="gamePiece">')
    }
    else if (z > 6) {
	  //first three rows
      $('#gameBoard tr:eq(0) td:odd').append('<div class="gamePiece">')
      $('#gameBoard tr:eq(1) td:even').append('<div class="gamePiece">')
      $('#gameBoard tr:eq(2) td:odd').append('<div class="gamePiece">')
      //last three rows
      $('#gameBoard tr:last td:even').append('<div class="gamePiece">')
      $('#gameBoard tr:nth-last-child(2) td:odd').append('<div class="gamePiece">')
      $('#gameBoard tr:nth-last-child(3) td:even').append('<div class="gamePiece">') 
    }

  })//onclick function
  
  
})//document
table {
  border: solid 1px black;
  border-spacing: 0px;
}
td {
  width: 50px;
  height: 50px;
}
.div1 {
  font-size: medium;
}
.gamePiece {
  border-radius: 100%;
  height: 100%;
  width: 100%;
  background-color: red;
}
<!DOCTYPE html>

<body>
  <h1>Game Board</h1>
  <table id="gameBoard">
    <tbody></tbody>
  </table>
  <script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
</body>
Run codeHide result

"" (, ) , "" , td . td/ "" 1 td: . td/ "" td: . , (1 1 ) , "1", "0". , .

- , ?

+4
2

boolean, , ?

"":

var toggleFlag = true;

for, "" td , :

//for each row add z number of td's
for (var j = 0; j < z; j++) {
    if(toggleFlag){
        array[i].append('<td class="odd"></td>');
    }else{
        array[i].append('<td class="even"></td>');
    }
    // Toggle the flag for next iteration
    toggleFlag=!toggleFlag;
}

, , td ... , , :

// If rows contain an even amout of td, toggle again before looping to next row
if(z % 2 == 0){
    toggleFlag=!toggleFlag;
}

, "Let play", , td, :odd :even.

$('#gameBoard tr:nth-last-child(3) td.odd').append('<div class="gamePiece">')

. CodePen
;)


EDIT,

, , :

$('.trOdd td:odd').css('background-color', 'black'); 
$('.trOdd td:even').css('background-color', 'white'); 
$('.trEven td:odd').css('background-color', 'white');
$('.trEven td:even').css('background-color', 'black'); 

.
.

, "-" .
" " ... .
.
, , td.

, CodePen.
, $('.trOdd td:odd') td, tr... ! , ... , . .

? jQuery (trOdd trEven)...
;)

+1

CSS. JS , CSS .

$(document).ready(function() {

    //create input form for number/size of board
    $('h1').append('<div class=div1>Size of Board: <input type="text" id = "size1" name="size2" min = "2" max = "100" step = "2" value = "6"><input type="submit" id="submit1" value="Create"></div>')

    //create button to print Game Pieces 
    $('h1').append('<div><input type="submit" id="submitForm" value="Lets Play!"></div>');


    var z = '';
    //on clicking the button, create array of empty boxes/<td>
    $('#submit1').click(function() {
      var array = [];
      //remove previous appended array
      $('tbody').empty();

      //grab current value or size of gameboard
      z = $('#size1').val();

      //with a for loop, create "empty" table (z by z) of boxes
      for (var i = 0; i < z; i++) {
        //addClass so we can grab later for color assignment
        var trEven = $('<tr>').addClass('trEven');
        var trOdd = $('<tr>').addClass('trOdd');
        //Differentiate between row to row: assign class trEven and trOdd to every other row      
        if (i % 2 == 0) {
          array.push(trEven);
        } else {
          array.push(trOdd);
        }
        //for each row add z number of td's
        for (var j = 0; j < z; j++) {
          array[i].append('<td></td>');
        }
      }

      //append updated array to <tbody>
      $('tbody').append(array);
      //select all evens/odds of trOdd/trEven to assign colors using special selectors
      $('.trOdd td:odd').addClass('even');
      $('.trOdd td:even').addClass('odd');
      $('.trEven td:odd').addClass('odd');
      $('.trEven td:even').addClass('even');

    }); //onclick function


    //Play Button: add game pieces
    $('#submitForm').click(function() {
        if (z <= 6) {
          //first two rows
          $('#gameBoard tr:eq(0) td:odd').append('<div class="gamePiece">')
          $('#gameBoard tr:eq(1) td:even').append('<div class="gamePiece">')
            //last two rows
          $('#gameBoard tr:last td:even').append('<div class="gamePiece">')
          $('#gameBoard tr:nth-last-child(2) td:odd').append('<div class="gamePiece">')
        } else if (z > 6) {
          //first three rows
          $('#gameBoard tr:eq(0) td:odd').append('<div class="gamePiece">')
          $('#gameBoard tr:eq(1) td:even').append('<div class="gamePiece">')
          $('#gameBoard tr:eq(2) td:odd').append('<div class="gamePiece">')
            //last three rows
          $('#gameBoard tr:last td:even').append('<div class="gamePiece">')
          $('#gameBoard tr:nth-last-child(2) td:odd').append('<div class="gamePiece">')
          $('#gameBoard tr:nth-last-child(3) td:even').append('<div class="gamePiece">')
        }

      }) //onclick function


  }) //document
table {
  border: 1px solid #000;
}

td {
  background-color: #fff;
  height: 50px;
  width: 50px;
}

td:nth-child(2n) {
  background-color: #000;
}

tr:nth-child(2n) td {
  background-color: #000;
}

tr:nth-child(2n) td:nth-child(2n) {
  background-color: #fff;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h1>Game Board</h1>
  <table id="gameBoard">
    <tbody></tbody>
  </table>
Hide result
+1

Source: https://habr.com/ru/post/1650106/


All Articles