-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
61 lines (56 loc) · 1.84 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//UI Vars
const game = document.querySelector('#game')
const minNum = document.querySelector('.minNum')
const maxNum = document.querySelector('.maxNum')
const guessInput = document.querySelector('.guessInput')
const guessBtn = document.querySelector('.guessSubmit')
const message = document.querySelector('.message')
//Game vars
generateRightNum = (min, max) => {
return (Math.floor(Math.random() * (max - min + 1) + 1))
}
let min = 1,
max = 10,
rightNum = generateRightNum(min, max),
guesesLeft = 3
minNum.textContent = min
maxNum.textContent = max
game.addEventListener('mousedown', (event) => {
if (event.target.classList.contains('playAgain')){
window.location.reload()
}
})
guessBtn.addEventListener('click',() => {
let guess = Number(guessInput.value)
if (isNaN(guess) || guess < min || guess > max){setMessage(`Please write number between ${min} and ${max}`, 'red')}
else {
if (guess === rightNum){
//Game Won
gameOver(true, `${rightNum} is correct!`)
}else{
//Wrong number
guesesLeft -= 1
if(guesesLeft === 0){
//Game over
gameOver(false, `GAME OVER. The correct answer was ${rightNum}`)
}else {
//Game continue - answer wrong
guessInput.style.borderColor = 'yellow'
guessInput.value = ''
setMessage(`${guess} is not correct, ${guesesLeft} tries left`, 'yellow')
}
}
}
})
gameOver = (isWin, msg) => {
let color = isWin ? 'green' : 'red'
guessInput.disabled = true
guessInput.style.borderColor = color
setMessage(msg, color)
guessBtn.value = 'Play Again'
guessBtn.className += ' playAgain'
}
setMessage = (msg, color = 'black') => {
message.textContent = msg
message.style.color = color
}