-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added more logic for when a number is correct or too high/low
- Loading branch information
1 parent
52e302f
commit 57acc54
Showing
1 changed file
with
25 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,35 @@ | ||
'use strict'; | ||
|
||
const secretNumber = Math.trunc(Math.random() * 20) + 1; | ||
let score = 20; | ||
document.querySelector('.number').textContent = secretNumber; | ||
|
||
document.querySelector('.check').addEventListener('click', function () { | ||
const guess = Number(document.querySelector('.guess').value); | ||
console.log(typeof guess); | ||
|
||
if (!guess) { | ||
document.querySelector('.message').textContent = | ||
'You need to enter a number! 😄'; | ||
} else if (guess === secretNumber) { | ||
document.querySelector('.message').textContent = | ||
'You entered the correct number! 🎉'; | ||
} else if (guess > secretNumber) { | ||
if (score > 1) { | ||
document.querySelector('.message').textContent = 'Too high, try again!'; | ||
score--; | ||
document.querySelector('.score').textContent = score; | ||
} else { | ||
document.querySelector('.message').textContent = 'You lost the game!'; | ||
document.querySelector('.score').textContent = '0'; | ||
} | ||
} else if (guess < secretNumber) { | ||
if (score > 1) { | ||
document.querySelector('.message').textContent = 'Too low, try again!'; | ||
score--; | ||
document.querySelector('.score').textContent = score; | ||
} else { | ||
document.querySelector('.message').textContent = 'You lost the game!'; | ||
document.querySelector('.score').textContent = '0'; | ||
} | ||
} | ||
}); |