didkoslawow

Untitled

Jan 21st, 2023 (edited)
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function chessBoard(rows) {
  2.   console.log('<div class="chessboard">');
  3.  
  4.   let currentSquareColor = 'black';
  5.   let priviousSquareColor = '';
  6.  
  7.   for (let i = 1; i <= rows; i++) {
  8.     console.log('  <div>');
  9.  
  10.     for (let j = 1; j <= rows; j++) {
  11.       console.log(`    <span class="${currentSquareColor}"></span>`);
  12.  
  13.       priviousSquareColor = currentSquareColor;
  14.       currentSquareColor = priviousSquareColor === 'black' ? 'white' : 'black';
  15.     }
  16.  
  17.     console.log('  </div>');
  18.  
  19.     if (rows % 2 === 0) {
  20.       priviousSquareColor = currentSquareColor;
  21.       currentSquareColor = priviousSquareColor === 'black' ? 'white' : 'black';
  22.     }
  23.   }
  24.   console.log('</div>');
  25. }
Advertisement
Add Comment
Please, Sign In to add comment