Skip to content
Open
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
28 changes: 17 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,26 @@ const maxNumberOfAttempts = 5;
// <- 32
// > getRandomNumber(1, 50)
// <- 11

function getRandomNumber(min, max) {
return Math.floor(Math.random() * (max - min)) + min;
}

function checkGuess() {
// Get value from guess input element
const guess = parseInt(guessInput.value, 10);
attempts = attempts + 1;


hideAllMessages();

if ( guess < 1 || guess > 99 || isNaN(guess) ) { // Stretch Goals: Input validation
console.log('Invalid guess');
return;
}
attempts = attempts + 1;

if (guess === targetNumber) {
numberOfGuessesMessage.style.display = '';
numberOfGuessesMessage.innerHTML = `You made ${attempts} guesses`;
numberOfGuessesMessage.innerHTML = `You made ${attempts} ${attempts === 1 ? 'guess' : 'guesses'}`; // Stretch Goals: If there is only one guess left, it should say "guess" (singular) instead of "guesses" (plural)

correctMessage.style.display = '';

Expand All @@ -43,16 +49,16 @@ function checkGuess() {
if (guess < targetNumber) {
tooLowMessage.style.display = '';
} else {
tooLowMessage.style.display = '';
tooHighMessage.style.display = ''; // Was an error here, should be tooHighMessage not tooLowMessage
}

const remainingAttempts = maxNumberOfAttempts - attempts;

numberOfGuessesMessage.style.display = '';
numberOfGuessesMessage.innerHTML = `You guessed ${guess}. <br> ${remainingAttempts} guesses remaining`;
numberOfGuessesMessage.innerHTML = `You guessed ${guess}. <br> ${remainingAttempts} ${remainingAttempts === 1 ? 'guess' : 'guesses'} remaining`; // Stretch Goals: If there is only one guess left, it should say "guess" (singular) instead of "guesses" (plural)
}

if (attempts ==== maxNumberOfAttempts) {
if (attempts === maxNumberOfAttempts) { // Was an error here, should be === not ====
submitButton.disabled = true;
guessInput.disabled = true;
}
Expand All @@ -63,21 +69,21 @@ function checkGuess() {
}

function hideAllMessages() {
for (let elementIndex = 0; elementIndex <= messages.length; elementIndex++) {
for (let elementIndex = 0; elementIndex < messages.length; elementIndex++) { // Was an error here, should be elementIndex < messages.length not elementIndex <= messages.length
messages[elementIndex].style.display = 'none';
}
}

funtion setup() {
function setup() { // Was an error here, should be function not funtion
// Get random number
targetNumber = getRandomNumber(1, 100);
targetNumber = getRandomNumber(1, 99); // Was an error here, should be 99 not 100
console.log(`target number: ${targetNumber}`);

// Reset number of attempts
maxNumberOfAttempts = 0;
attempts = 0; // Was an error here, should be attempts not maxNumberOfAttempts

// Enable the input and submit button
submitButton.disabeld = false;
submitButton.disabled = false; // Was an error here, should be disabled not disabeld
guessInput.disabled = false;

hideAllMessages();
Expand Down