Advertisement
PowerCell46

Print chessboard JS

Nov 23rd, 2022
698
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function chessBoard(number) {
  2.   console.log(`<div class="chessboard">`);
  3.  
  4.   let oddOutput = `   <div>`;
  5.   for (let index = 1; index <= number; index++) {
  6.     if (index % 2 === 0) {
  7.       oddOutput += `\n  <span class="white"></span>`;
  8.     } else {
  9.       oddOutput += `\n  <span class="black"></span>`;
  10.     }
  11.     index++;
  12.     if (index === number + 1) {
  13.       oddOutput += `\n   </div>`;
  14.       index--;
  15.     } else {
  16.       index--;
  17.     }
  18.   }
  19.  
  20.   let evenOutput = `  <div>`;
  21.   for (let index = 2; index <= number + 1; index++) {
  22.     if (index % 2 === 0) {
  23.       evenOutput += `\n <span class="white"></span>`;
  24.     } else {
  25.       evenOutput += `\n <span class="black"></span>`;
  26.     }
  27.     index++;
  28.     if (index === number + 2) {
  29.       evenOutput += `\n   </div>`;
  30.       index--;
  31.     } else {
  32.       index--;
  33.     }
  34.   }
  35.  
  36.   for (
  37.     let currentLinePrint = 1;
  38.     currentLinePrint <= number;
  39.     currentLinePrint++
  40.   ) {
  41.     if (currentLinePrint % 2 !== 0) {
  42.       console.log(oddOutput);
  43.     } else {
  44.       console.log(evenOutput);
  45.     }
  46.  
  47.     currentLinePrint++;
  48.     if (currentLinePrint === number + 1) {
  49.       console.log(` </div>`);
  50.       currentLinePrint--;
  51.     } else {
  52.       currentLinePrint--;
  53.     }
  54.   }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement