|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +// Generate a random secret number between 1 and 20 |
| 4 | +let secretNumber = Math.trunc(Math.random() * 20) + 1; |
| 5 | + |
| 6 | +// Initialize score and highest score variables |
| 7 | +let score = 20; |
| 8 | +let highestScore = 0; |
| 9 | + |
| 10 | +// Set initial score on UI |
| 11 | +document.querySelector('.score').textContent = score; |
| 12 | + |
| 13 | +// Utility function to display messages to the user |
| 14 | +const displayMessage = function (message) { |
| 15 | + document.querySelector('.message').textContent = message; |
| 16 | +}; |
| 17 | + |
| 18 | +// Event listener for the "Check!" button |
| 19 | +document.querySelector('.check').addEventListener('click', function () { |
| 20 | + // Get user's guess from input field and convert it to a number |
| 21 | + const guessNumber = Number(document.querySelector('.guess').value); |
| 22 | + |
| 23 | + // If input is empty or zero (falsy value) |
| 24 | + /* Feature Challenge #1: Limit the Guessing Range |
| 25 | +1. Inside your click event handler for the 'Check!' button, after converting the input to a number: |
| 26 | +2. Add a condition to check if the number is less than 1 or greater than 20 |
| 27 | +3. If it is, use displayMessage() to show something like "⛔ Enter a number between 1 and 20!" |
| 28 | +4. Make sure to return early so the rest of the code does not run |
| 29 | +5. This will prevent the game from counting invalid inputs as a real attempt |
| 30 | +*/ |
| 31 | + if (!guessNumber || guessNumber < 1 || guessNumber > 20) { |
| 32 | + displayMessage('No Number Found'); |
| 33 | + } else if (guessNumber === secretNumber) { |
| 34 | + // Reveal the secret number |
| 35 | + document.querySelector('.number').textContent = secretNumber; |
| 36 | + |
| 37 | + // Show success message |
| 38 | + displayMessage('Hurray! You matched the secret number'); |
| 39 | + |
| 40 | + // Change background and number box styling |
| 41 | + document.querySelector('body').style.backgroundColor = '#60b347'; |
| 42 | + document.querySelector('.number').style.width = '30rem'; |
| 43 | + |
| 44 | + // Update high score if current score is greater |
| 45 | + if (score > highestScore) { |
| 46 | + highestScore = score; |
| 47 | + document.querySelector('.highscore').textContent = highestScore; |
| 48 | + } |
| 49 | + |
| 50 | + // If guess is incorrect (either too high or too low) |
| 51 | + } else if (guessNumber !== secretNumber) { |
| 52 | + // If player still has remaining score |
| 53 | + if (score > 1) { |
| 54 | + // Show hint and reduce score |
| 55 | + displayMessage(guessNumber > secretNumber ? 'Too High' : 'Too Low'); |
| 56 | + score--; |
| 57 | + document.querySelector('.score').textContent = score; |
| 58 | + } else { |
| 59 | + // If no score left, player loses |
| 60 | + displayMessage('Alas! You lost the game'); |
| 61 | + document.querySelector('.score').textContent = 0; |
| 62 | + } |
| 63 | + } |
| 64 | +}); |
| 65 | + |
| 66 | +// Event listener for the "Again!" button (reset the game) |
| 67 | +document.querySelector('.again').addEventListener('click', function () { |
| 68 | + // Generate new secret number |
| 69 | + secretNumber = Math.trunc(Math.random() * 20) + 1; |
| 70 | + |
| 71 | + // Reset score to 20 |
| 72 | + score = 20; |
| 73 | + |
| 74 | + // Restore UI to initial state |
| 75 | + document.querySelector('body').style.backgroundColor = '#222'; |
| 76 | + displayMessage('Start guessing.....'); |
| 77 | + document.querySelector('.guess').value = ''; |
| 78 | + document.querySelector('.number').textContent = '?'; |
| 79 | + document.querySelector('.number').style.width = '15rem'; |
| 80 | + document.querySelector('.score').textContent = score; |
| 81 | +}); |
| 82 | + |
| 83 | + |
| 84 | +/* |
| 85 | +1. Inside your click event handler for the 'Check!' button, after converting the input to a number: |
| 86 | +2. Add a condition to check if the number is less than 1 or greater than 20 |
| 87 | +3. If it is, use displayMessage() to show something like "⛔ Enter a number between 1 and 20!" |
| 88 | +4. Make sure to return early so the rest of the code does not run |
| 89 | +5. This will prevent the game from counting invalid inputs as a real attempt |
| 90 | +*/ |
0 commit comments