Concluí o projeto e tentei adicionar uma nova funcionalidade para limitar o número de tentativas mas não está funcionando como deveria, alguém poderia me auxiliar por favor?
alert('Welcome to the secret number game.\nYou have 3 attempts.');
let maximumAttempts = 3;
let secretNumber = parseInt(Math.random() * 10 + 1); // Returns a random integer from 1 to 100
console.log(secretNumber);
let chosenNumber;
let attempts = 1;
while ((chosenNumber != secretNumber) && (attempts <= maximumAttempts)) {
chosenNumber = prompt('Choose a number from 1 to 10: ');
if (secretNumber == chosenNumber) {
break;
} else {
if (secretNumber > chosenNumber){
alert(`The Secret number is higher than ${chosenNumber}`);
} else {
alert(`The Secret number is lower than ${chosenNumber}`);
}
attempts++;
}
}
if (attempts === maximumAttempts){
alert(`You ran out of guesses! The secret number was ${secretNumber}`);
} else {
let wordAttempt = attempts > 1 ? 'attempts' : 'attempt';
alert(`Correct! You have discovered the Secret Number ${secretNumber} with ${attempts} ${wordAttempt}.`);
}
let wordAttempt = attempts > 1 ? 'attempts' : 'attempt';
alert(`Correct! You have discovered the Secret Number ${secretNumber} with ${attempts} ${wordAttempt}.`);