From 86f387f2856b21c9d0bc40b50f138d35097ee3cc Mon Sep 17 00:00:00 2001 From: Zhana Kusman Date: Tue, 12 Dec 2023 11:56:42 -0800 Subject: [PATCH] section-5 --- .vscode/settings.json | 3 +++ index.js | 20 ++++++++++---------- 2 files changed, 13 insertions(+), 10 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..6f3a291 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "liveServer.settings.port": 5501 +} \ No newline at end of file diff --git a/index.js b/index.js index f3bc6d8..a1ad99f 100644 --- a/index.js +++ b/index.js @@ -5,12 +5,12 @@ const messages = document.getElementsByClassName('message'); const tooHighMessage = document.getElementById('too-high'); const tooLowMessage = document.getElementById('too-low'); const maxGuessesMessage = document.getElementById('max-guesses'); -const numberOfGuessesMessage = document.getElementById('num-of-guesses'); +const numberOfGuessesMessage = document.getElementById('number-of-guesses'); const correctMessage = document.getElementById('correct'); let targetNumber; -const attempts = 0; -const maxNumberOfAttempts = 5; +let attempts = 0; +let maxNumberOfAttempts = 5; // Returns a random number from min (inclusive) to max (exclusive) // Usage: @@ -25,7 +25,7 @@ function getRandomNumber(min, max) { function checkGuess() { // Get value from guess input element const guess = parseInt(guessInput.value, 10); - attempts = attempts + 1; + attempts += 1; hideAllMessages(); @@ -43,7 +43,7 @@ function checkGuess() { if (guess < targetNumber) { tooLowMessage.style.display = ''; } else { - tooLowMessage.style.display = ''; + tooHighMessage.style.display = ''; } const remainingAttempts = maxNumberOfAttempts - attempts; @@ -52,7 +52,7 @@ function checkGuess() { numberOfGuessesMessage.innerHTML = `You guessed ${guess}.
${remainingAttempts} guesses remaining`; } - if (attempts ==== maxNumberOfAttempts) { + if (attempts === maxNumberOfAttempts) { submitButton.disabled = true; guessInput.disabled = true; } @@ -63,21 +63,21 @@ function checkGuess() { } function hideAllMessages() { - for (let elementIndex = 0; elementIndex <= messages.length; elementIndex++) { + for (let elementIndex = 0; elementIndex < messages.length; elementIndex++) { messages[elementIndex].style.display = 'none'; } } -funtion setup() { +function setup() { // Get random number targetNumber = getRandomNumber(1, 100); console.log(`target number: ${targetNumber}`); // Reset number of attempts - maxNumberOfAttempts = 0; + attempts = 0; // Enable the input and submit button - submitButton.disabeld = false; + submitButton.disabled = false; guessInput.disabled = false; hideAllMessages();