Advertisement
ZEdKasat

Guessing Game

Dec 9th, 2021
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 2.12 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <title>Guess the Number</title>
  5.     <style>
  6.  
  7.         body{
  8.             background-color: darkslategray;
  9.             color: white;
  10.             padding: 10%;
  11.         }
  12.  
  13.         #title{
  14.             background-color: darkturquoise;
  15.             padding: 20px;
  16.             border-radius: 10px;
  17.         }
  18.  
  19.  
  20.         .btn{
  21.             width: 25%;
  22.             background-color: dimgray;
  23.             color: whitesmoke;
  24.         }
  25.  
  26.         #check{
  27.             padding: 10px;
  28.             border-radius: 10px;
  29.         }
  30.  
  31.     </style>
  32. </head>
  33. <body>
  34.     <h1 id="title">Guess a number between 1-20</h1>
  35.     <input type="number" id="numinput">
  36.     <button id="check" class="btn">Check</button>
  37.     <p id="message">Select a random number now! </p>
  38.     <p id="score">Your Score is 20!</p>
  39.     <script>
  40.         var random_num = Math.ceil(Math.random() * 20);
  41.         console.log(random_num);
  42.         let score = 20;
  43.  
  44.         function buttonPressed(){
  45.             // console.log("Button Pressed Function")
  46.            
  47.             var guessed_value = Number(document.querySelector("#numinput").value);
  48.             var title_element = document.querySelector("#title");
  49.             var message_element = document.querySelector("#message");
  50.  
  51.             if (guessed_value < random_num) {
  52.                title_element.style.backgroundColor = "blue";
  53.                message_element.textContent = "Go Higher!";
  54.            } else if (guessed_value > random_num){
  55.                 title_element.style.backgroundColor = "red";
  56.                 message_element.textContent = "Go Lower!";
  57.             } else {
  58.                 title_element.style.backgroundColor = "green";
  59.                 message_element.textContent = "Woah! You are a mind reader!";
  60.             }
  61.             score -= 1;
  62.             document.querySelector("#score").textContent = 'Your Score is ${score}';
  63.            
  64.            
  65.             // title_element.style.backgroundColor = "red";
  66.         }
  67.  
  68.         document.querySelector("#check").addEventListener('click', buttonPressed);
  69.  
  70.     </script>
  71. </body>
  72. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement