-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfunction_dom.js
61 lines (52 loc) · 1.43 KB
/
function_dom.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
let correctAnswer
let numberOfGuesses
let getUserGuess = function() {
let input = document.getElementById('input')
let guess = input.value
input.value = ''
compareGuess(guess)
}
let compareGuess = function(guess) {
numberOfGuesses++
if (guess > correctAnswer) {
renderOuput(`
<strong>Too high</strong>
<br />
Number of guesses: ${numberOfGuesses}
`)
} else if (guess < correctAnswer) {
renderOuput(`
<strong>Too low</strong>
<br />
Number of guesses: ${numberOfGuesses}
`)
} else {
renderOuput(`
<strong>You guessed it!</strong>
<br />
It took you ${numberOfGuesses} tries
`)
}
}
let renderOuput = function(message) {
let output = document.getElementById('output')
output.innerHTML = message
}
let reset = function() {
correctAnswer = Math.floor(Math.random() * 100) + 1
numberOfGuesses = 0
renderOuput('')
}
let checkIfEnterPressed = function(event) {
if (event.keyCode === 13) {
event.preventDefault()
getUserGuess()
}
}
let button = document.getElementById('submit')
button.addEventListener('click', getUserGuess)
let input = document.getElementById('input')
input.addEventListener('keydown', checkIfEnterPressed)
let resetButton = document.getElementById('reset')
resetButton.addEventListener('click', reset)
reset()