-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
50 lines (35 loc) · 1.04 KB
/
index.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
// Generate a Random Number between 1 and 100
function generateNumber() {
// Math.random is 0 -> .99999999
return Math.floor((Math.random() * 100) + 1)
}
function checkGuess(guess, answer) {
if (guess === answer){
// correct answer
return 'Correct'
} else if (guess < answer) {
return 'Too Low'
} else {
return 'Too High'
}
}
let button = document.querySelector('button')
button.addEventListener('click', function() {
if (numberOfGuesses < 5) {
// Grab the input field value
let input = document.getElementById('guess')
let guess = parseInt(input.value)
// check that value against the randomNumber
let result = checkGuess(guess, randomNumber)
// Display the result to the user
let resultsSection = document.getElementById('result')
resultsSection.innerText = result
console.log(result)
numberOfGuesses++
}
})
function startNewGame() {
numberOfGuesses = 0
randomNumber = generateNumber()
}
startNewGame()