-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
66 lines (58 loc) · 2.1 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
62
63
64
65
66
document.addEventListener('DOMContentLoaded', () => {
// grab elements & store in variable
const header = document.querySelector('#header')
const form = document.querySelector('#guessNum')
const inputNum = document.querySelector('#input-number')
const success = document.querySelector('#success')
const hint = document.querySelector('#hint')
const showNum = document.querySelector('.show-num')
const enter = document.querySelector('#enter')
const reset = document.querySelector('#reset')
let generateRandomNum = Math.floor(Math.random() * 30) + 1
console.log(`INIT: ${generateRandomNum}`)
let tries = 1
reset.style.visibility = 'hidden'
form.addEventListener('submit', (e) => {
e.preventDefault()
// get input value
const getNum = parseFloat(e.target.getNum.value)
if (getNum < generateRandomNum) {
tries++
success.textContent = ''
e.target.getNum.value = ''
hint.textContent = 'Oops... Maybe higher number?'
enter.innerText = 'Try Again'
} else if (getNum > generateRandomNum) {
tries++
success.textContent = ''
e.target.getNum.value = ''
hint.textContent = "Hmmm... Let's try lower number"
enter.innerText = 'Try Again'
} else {
header.innerHTML = '<span class="message">Yes! The mystery number is:</span>'
success.innerHTML = `You got me after <span class="bigger-font">${tries}</span> times!`
hint.textContent = ''
e.target.getNum.value = ''
showNum.textContent = `${getNum}`
reset.style.visibility = 'visible'
enter.style.visibility = 'hidden'
inputNum.style.visibility = 'hidden'
}
console.log(`TRY: ${tries}`)
})
reset.addEventListener('click', (e) => {
e.preventDefault()
reset.style.visibility = 'hidden'
enter.style.visibility = 'visible'
inputNum.style.visibility = 'visible'
document.querySelector('input').value = ''
success.textContent = ''
showNum.textContent = ''
enter.textContent = "Let's play!"
header.textContent = 'Choose a number between 1 and 30'
generateRandomNum = Math.floor(Math.random() * 30) + 1
tries = 1
console.log(`from reset: ${generateRandomNum}`)
console.log(`tries from reset: ${tries}`)
})
})