vikkktor

Data Types and Var_ChessBoard

Oct 12th, 2021
849
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //------------------------------------------
  2. // Write a function to print a chessboard of size n X n. See the example for more information.
  3. // The input comes as a single number argument n.
  4. // The output should be returned as a result of your function in the form of a string.
  5. //------------------------------------------
  6.  
  7. function chessBoard(n) {
  8.     let size = n;
  9.     let board = "<div class=\"chessboard\">\n";
  10.  
  11.     for (let y = 0; y < size; y++) {
  12.         board += "  <div>\n"
  13.  
  14.         for (let x = 0; x < size; x++) {
  15.             if ((x + y) % 2 == 0)
  16.                 board += "    <span class=\"black\"></span>\n";
  17.             else
  18.                 board += "    <span class=\"white\"></span>\n";
  19.         }
  20.         board += "  </div>\n";
  21.     }
  22.     board += "</div>";
  23.     console.log(board);
  24.  
  25. }
  26. chessBoard(3)
RAW Paste Data