How to stop a while loop after 3 tries?
After 3 tries the game should stop and the player loses.
let num = 4 is the correct number.
I will change it later for Math.floor(Math.random)
const btn = document.querySelector('.btn')
const result = document.querySelector('.output')
let num = 4 // guessed number .
btn.addEventListener('click', function() {
let tries = 3;
while (tries > 0) {
const input = document.querySelector('.input').value
if (input == num) {
result.innerHTML = `You win! Correct number was ${num}.`
} else if (input > num) {
result.innerHTML = `Your guess is hight!`
} else if (input < num) {
result.innerHTML = `Your guess is low!`
}
tries = tries - 1;
}
document.querySelector('.input').value = ''
})
<input type="number" class="input">
<button class="btn">Submit</button>
<div class="output"></div>