Advertisement
ZEdKasat

Untitled

Dec 31st, 2021
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 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" max="20" min="1">
  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. var score = 20;
  42.  
  43. function buttonPressed(){
  44. var guessed_value = Number(document.querySelector("#numinput").value);
  45. var title_element = document.querySelector("#title");
  46. var message_element = document.querySelector("#message");
  47.  
  48. if (guessed_value < random_num) {
  49. title_element.style.backgroundColor = "blue";
  50. message_element.textContent = "Go Higher!";
  51. } else if (guessed_value > random_num){
  52. title_element.style.backgroundColor = "red";
  53. message_element.textContent = "Go Lower!";
  54. } else {
  55. title_element.style.backgroundColor = "green";
  56. message_element.textContent = "Woah! You are a mind reader!";
  57. }
  58. score -= 1;
  59. document.querySelector("#score").textContent = `Your Score is ${score}`;
  60. }
  61.  
  62. document.querySelector("#check").addEventListener('click', buttonPressed);
  63.  
  64. </script>
  65. </body>
  66. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement