-
Notifications
You must be signed in to change notification settings - Fork 0
/
guessingGame.js
50 lines (40 loc) · 1.56 KB
/
guessingGame.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
let input = document.getElementById('input');
const button = document.getElementById('btn');
const output = document.getElementById('output');
const inputDiv = document.getElementById('inputDiv');
let wrongNumber = document.getElementById('numberOfTries');
output.classList.add('classResult');
output.innerHTML = 'good luck!';
// shows us how many times we have tried to find the number (line 22)
numberOfTries = 0;
// Random number between 1 and 100
const randomNumb = Math.floor(Math.random() * 100);console.log(randomNumb);
button.addEventListener('click', () => {
input = document.getElementById('input').value;
output.classList.add('classResult');
// whenever the numbers do not match, we add +1 on numbersOfTries
if(input !== randomNumb){
numberOfTries++
console.log(numberOfTries)
wrongNumber.innerText = numberOfTries;
};
// When the numbers match
if(input == randomNumb){
output.innerHTML = `You guessed the number after ${numberOfTries} tries`;
inputDiv.classList.add('classDiv');
inputDiv.innerHTML = 'Congratulations';
}
// After ten tries the game is over
if(numberOfTries == 10){
output.remove();
inputDiv.remove();
container.classList.add('classDiv');
container.innerHTML = 'You lost the game';
}
if(input < randomNumb){
output.innerHTML = `The number you are looking for is greater then ${input}`;
}
if(input > randomNumb){
output.innerHTML = `The number you are looking for is less then ${input}`;
}
});