OrangoMango

GuessNumber game tutorial

May 14th, 2020
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 1.30 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3.   <head>
  4.     <title>GuessNumber</title>
  5.     <script>
  6.       var correct = Math.ceil(Math.random() * 10)
  7.       var attemps = 0
  8.      
  9.       function validate(){
  10.         var t = parseInt(document.getElementById("textinput").value, 10)
  11.         if (correct == t){
  12.           alert("You guessed the right number in "+attemps+" attemps")
  13.           return true
  14.         } else if (t > correct){
  15.           alert("Try lower")
  16.         } else if (t < correct){
  17.          alert("Try higher")
  18.        } else {
  19.          alert("Invalid input")
  20.        }
  21.        attemps++
  22.        return false
  23.      }
  24.    </script>
  25.     <style>
  26.       input[type=submit]{
  27.         border: solid;
  28.         margin-top: 14px;
  29.         margin-left: 60px;
  30.         width: 50px;
  31.         height: 30;
  32.       }
  33.      
  34.       #textinput{
  35.         border: solid;
  36.         height: 30px;
  37.         width: 180px;
  38.       }
  39.      
  40.       body{
  41.         position: absolute;
  42.         right: 30%;
  43.         top: 30%;
  44.         background-color: red;
  45.       }
  46.     </style>
  47.   </head>
  48.   <body>
  49.     <h2>Guess the number between 0 and 100</h2>
  50.     <form onsubmit="return validate()">
  51.       <input type="number" id="textinput" placeholder="Enter a number" required />
  52.       <br /><input type="submit" />
  53.     </form>
  54.   </body>
  55. </html>
Advertisement
Add Comment
Please, Sign In to add comment