Liliana797979

chess board1 - fundamentals

May 28th, 2021
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function chessBoard(n) {
  2.     let result = '<div class="chessboard"> \n';
  3.     let color = 'black';
  4.  
  5.     for (let i = 0; i < n; i++) {
  6.         result += '  <div> \n';
  7.  
  8.         if (n % 2 === 0) {
  9.             if (color === 'black') {
  10.                 color = 'white';
  11.             } else {
  12.                 color = 'black';
  13.             }
  14.         }
  15.  
  16.         if (i === 0) {
  17.             color = 'black';
  18.         }
  19.  
  20.         for (let j = 0; j < n; j++) {
  21.             result += `    <span class="${color}"></span> \n`;
  22.  
  23.             if (color === 'black') {
  24.                 color = 'white';
  25.             } else {
  26.                 color = 'black';
  27.             }
  28.         }
  29.  
  30.         result += '  </div> \n';
  31.     }
  32.  
  33.     result += '</div>';
  34.  
  35.     console.log(result);
  36. }
Advertisement
Add Comment
Please, Sign In to add comment