Advertisement
ZEdKasat

Javascript Demo

Dec 4th, 2021
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 2.70 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.     <title>Guess Random number</title>
  5.     <style>
  6.         body {
  7.             background-color: #800080;
  8.             color: white;
  9.             font-family: fantasy;
  10.             padding: 50px;
  11.         }
  12.     </style>
  13. </head>
  14. <body>
  15.     <h1 class="heading"> Guess a Random Number 1-20</h1>
  16.     <input type="number" class="gnum"><br>
  17.     <button class="check">Check</button>
  18.     <button class="reset">Reset</button><br>
  19.     <p class="message">Enter any number!!</p>
  20.     <p class="score">Score: 20</p>
  21.     <p class="highscore">HighScore: 0</p>
  22.     <script>
  23.         let winningnumber = Math.trunc((Math.random() * 20))
  24.         console.log(winningnumber)
  25.         let score = 20
  26.         let highscore = 0
  27.  
  28.         function gameplay() {
  29.             const guess = Number(document.querySelector('.gnum').value)
  30.             if (guess == winningnumber) {
  31.                 document.querySelector('.message').textContent = "You guessed correctly!!"
  32.                 //CSS manipulation is done like this
  33.                 //- is not allowed, instead camel casing is used
  34.                 document.querySelector('body').style.backgroundColor = '#65FF66'
  35.                 if (score > highscore) {
  36.                     highscore = score
  37.                     document.querySelector('.highscore').textContent = `HighScore: ${highscore}`
  38.                 }
  39.             } else if (guess > winningnumber) {
  40.                 document.querySelector('.message').textContent = "Go lower!"
  41.                 score -= 1
  42.                 document.querySelector('.score').textContent = `Score: ${score}`
  43.             } else if (guess < winningnumber) {
  44.                document.querySelector('.message').textContent = "Go higher!"
  45.                score -= 1
  46.                document.querySelector('.score').textContent = `Score: ${score}`
  47.            }
  48.  
  49.        }
  50.  
  51.        function reset() {
  52.            winningnumber = Math.trunc((Math.random() * 20))
  53.            console.log(winningnumber)
  54.            score = 20
  55.            // document.querySelector('body').style.backgroundColor = 'purple'
  56.            document.querySelector('.message').textContent = "Enter any number!!"
  57.            document.querySelector('.gnum').value = ""
  58.            document.querySelector('body').style.backgroundColor = '#800080'
  59.            document.querySelector('.score').textContent = `Score: ${score}`
  60.            document.querySelector('.highscore').textContent = `HighScore: ${highscore}`
  61.        }
  62.  
  63.        document.querySelector('.check').addEventListener(
  64.            'click', gameplay
  65.        )
  66.  
  67.        document.querySelector('.reset').addEventListener(
  68.            'click', reset
  69.        )
  70.    </script>
  71. </body>
  72.  
  73. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement