Advertisement
ZEdKasat

Javascript Demo

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