Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

section-5 #38

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"liveServer.settings.port": 5501
}
20 changes: 10 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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();

Expand All @@ -43,7 +43,7 @@ function checkGuess() {
if (guess < targetNumber) {
tooLowMessage.style.display = '';
} else {
tooLowMessage.style.display = '';
tooHighMessage.style.display = '';
}

const remainingAttempts = maxNumberOfAttempts - attempts;
Expand All @@ -52,7 +52,7 @@ function checkGuess() {
numberOfGuessesMessage.innerHTML = `You guessed ${guess}. <br> ${remainingAttempts} guesses remaining`;
}

if (attempts ==== maxNumberOfAttempts) {
if (attempts === maxNumberOfAttempts) {
submitButton.disabled = true;
guessInput.disabled = true;
}
Expand All @@ -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();
Expand Down