-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
84 lines (73 loc) · 3.14 KB
/
app.js
File metadata and controls
84 lines (73 loc) · 3.14 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
let secretNumber = Math.floor(Math.random()*20)+1;
let attempts = 10;
let currentScore = 20;
let highestScore = 0;
let input = document.getElementById('input');
let checkBtn = document.getElementById('check');
let attemptsElement = document.getElementById('attempts');
let gameMessage = document.getElementById('game-message');
let ansBox = document.getElementById('ans-box');
let body = document.querySelector('body');
let currentScoreElement = document.getElementById('current-score');
let highestScoreElement = document.getElementById('highest-score');
let newGame = document.getElementById('new-game');
//on clicking check button
checkBtn.addEventListener('click', function(){
let userInput = input.value;
attempts--;
currentScore--;
if(userInput>0 && userInput<=20){
if(attempts>0){
if(userInput == secretNumber){
gameMessage.textContent = `Correct guess!`;
attemptsElement.textContent = attempts;
ansBox.textContent = `${userInput}`;
currentScoreElement.textContent = currentScore+1;
body.style.backgroundColor = "green";
highestScore = currentScore > highestScore ? currentScore : highestScore;
highestScoreElement.textContent = highestScore+1;
}
else{
attemptsElement.textContent = attempts;
if(userInput<secretNumber){
gameMessage.textContent = `Incorrect guess! Try number larger than ${userInput}`;
currentScoreElement.textContent = currentScore;
attemptsElement.textContent = attempts;
body.style.backgroundColor = "red";
setTimeout(() => {
body.style.backgroundColor = "black";
}, 200);
}
else if(userInput>secretNumber){
gameMessage.textContent = `Incorrect guess! Try number smaller than ${userInput}`;
currentScoreElement.textContent = currentScore;
attemptsElement.textContent = attempts;
body.style.backgroundColor = "red";
setTimeout(() => {
body.style.backgroundColor = "black";
}, 200);
}
}
}
else{
console.log("You Lost")
}
}
else{
gameMessage.textContent = `Enter Number Between 1-20`;
attemptsElement.textContent = attempts;
currentScoreElement.textContent = currentScore;
}
})
//on clicking New Game button, reset all values, except Highest Score
newGame.addEventListener('click', ()=>{
body.style.backgroundColor = "black";
attempts = 10;
attemptsElement.textContent = attempts;
currentScore = 20;
currentScoreElement.textContent = currentScore;
input.value = '';
secretNumber = Math.floor(Math.random()*20)+1;
ansBox.textContent = "?";
gameMessage.textContent= "Guess a number between 1-20";
})