Guest User

FizzBuzz

a guest
Jun 8th, 2018
656
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 1.40 KB | None | 0 0
  1. <!DOCTYPE html>
  2.  
  3. <html>
  4.  
  5.   <head>
  6.     <meta charset="utf-8">
  7.     <title>FizzBuzz</title>
  8.   </head>
  9.  
  10.   <body>
  11.  
  12.     <!-- A button to begin the program -->
  13.  
  14.     <button onclick="generator()">Set the limit!</button>
  15.     <br>
  16.  
  17.     <!-- This is where the strings will appear -->
  18.  
  19.     <p id="inside"></p>
  20.  
  21.     <!-- This is the code that excecutes the loop -->
  22.  
  23.     <script>
  24.  
  25.       function generator() {
  26.  
  27.         // Variables that are for the code only
  28.  
  29.         var text = "";
  30.         var i = 1;
  31.  
  32.         // Variable that comes from the prompt input
  33.  
  34.         var limit = prompt("Count until...?", 100);
  35.         var tops = parseInt(limit) + 1;
  36.  
  37.         if ((tops > 0) && (tops % 1 == 0) && !(tops==NaN)) {
  38.          while (i < tops) {
  39.            if (i % 15 == 0) {
  40.              text += "FizzBuzz<br>";
  41.               i++;
  42.             }
  43.             else if (i % 3 == 0) {
  44.               text += "Fizz<br>";
  45.               i++;
  46.             }
  47.             else if (i % 5 == 0) {
  48.               text += "Buzz<br>";
  49.               i++;
  50.             }
  51.             else {
  52.               text += i + "<br>";
  53.               i++;
  54.             }
  55.           }
  56.         }
  57.         else {
  58.           text = "Only integers above zero please."
  59.         }
  60.  
  61.         // Printing result in the page
  62.  
  63.         document.getElementById("inside").innerHTML = text;
  64.  
  65.       }
  66.  
  67.     </script>
  68.  
  69.   </body>
  70.  
  71. </html>
Advertisement
Add Comment
Please, Sign In to add comment