Guest User

Untitled

a guest
Jun 8th, 2018
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 2.67 KB | None | 0 0
  1. <!DOCTYPE html>
  2.  
  3. <html>
  4.  
  5.   <head>
  6.     <meta charset="utf-8">
  7.     <title>Chessboard</title>
  8.   </head>
  9.  
  10.   <body>
  11.  
  12.     <!-- A button to display width -->
  13.  
  14.     <button onclick="widthCH()">Gimme width!</button>
  15.     <br>
  16.     <p id="widthDP"></p>
  17.  
  18.     <!-- A button to display lenght -->
  19.  
  20.     <button onclick="heightCH()">Gimme height!</button>
  21.     <br>
  22.     <p id="heightDP"></p>
  23.  
  24.     <!-- A button to run the code -->
  25.  
  26.     <button onclick="chessboard()">Go!</button>
  27.     <br>
  28.  
  29.     <!-- This is where the strings will appear -->
  30.  
  31.     <p id="chess"></p>
  32.  
  33.     <script>
  34.  
  35.       var widthVal = 8;
  36.       var heightVal = 8;
  37.  
  38.       // This is the code that collects width data
  39.  
  40.       function widthCH() {
  41.         widthVal = prompt("Define width.", 8);
  42.       }
  43.  
  44.       // This is the code that collects width data
  45.  
  46.       function heightCH() {
  47.         heightVal = prompt("Define height.", 8);
  48.       }
  49.  
  50.       // This is that excecutes the program
  51.  
  52.       function chessboard() {
  53.  
  54.         var row_even = "";
  55.         var row_odd = "";
  56.         var board = "";
  57.         var i = 0;
  58.  
  59.         // This generates the rows
  60.  
  61.         if ((widthVal > 0) && (widthVal % 1 == 0) && !(widthVal == NaN)) {
  62.  
  63.          // This generates the even rows
  64.  
  65.          while (i < widthVal) {
  66.            if (i % 2 == 0) {
  67.              row_even += "_";
  68.               i++;
  69.             }
  70.             else {
  71.               row_even += "#";
  72.               i++;
  73.             }
  74.           }
  75.  
  76.           // This resets the counter
  77.  
  78.           i = 0;
  79.  
  80.           // This generates the odd rows
  81.  
  82.           while (i < widthVal) {
  83.            if (i % 2 == 0) {
  84.              row_odd += "#";
  85.              i++;
  86.            }
  87.            else {
  88.              row_odd += "_";
  89.              i++;
  90.            }
  91.          }
  92.  
  93.          // This resets the counter
  94.  
  95.          i = 0;
  96.        }
  97.  
  98.        else {
  99.          widthVal = "nani? :V";
  100.        }
  101.  
  102.        // This creates the chessboard using the generated rows
  103.  
  104.        if ((heightVal > 0) && (heightVal % 1 == 0) && !(heightVal == NaN)) {
  105.          while (i < heightVal) {
  106.            if (i % 2 == 0) {
  107.              board += row_even + "<br>";
  108.               i++;
  109.             }
  110.             else {
  111.               board += row_odd + "<br>";
  112.               i++;
  113.             }
  114.           }
  115.         }
  116.         else {
  117.           heightVal = "nani? :V";
  118.         }
  119.  
  120.         document.getElementById("widthDP").innerHTML = "Width is: " + widthVal;
  121.         document.getElementById("heightDP").innerHTML = "Height is: " + heightVal;
  122.         document.getElementById("chess").innerHTML = board;
  123.  
  124.       }
  125.  
  126.     </script>
  127.  
  128.   </body>
  129. </html>
Advertisement
Add Comment
Please, Sign In to add comment