-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
48 lines (39 loc) · 1.54 KB
/
script.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
'use strict';
let secretNumber = Math.trunc(Math.random()*20)+1;
let score = 20;
let highscore = 0;
const displayMessage = function(message){
document.querySelector('.message').textContent = message;
}
document.querySelector('.check').addEventListener('click',function (){
const guess = Number(document.querySelector('.guess').value);
if(!guess){//Empty
displayMessage('No Number');
}else if(guess === secretNumber){//Win
displayMessage("Correct Number");
document.querySelector('body').style.backgroundColor = '#60b347'
document.querySelector('.number').style.width = '30rem'
document.querySelector('.number').textContent = secretNumber;
if(score > highscore){
highscore = score;
document.querySelector('.highscore').textContent = highscore;
}
}else if(guess != secretNumber){// High-Low
if(score > 1){
displayMessage(guess > secretNumber ? "Too High" : "Too Low");
score--;
document.querySelector('.score').textContent = score;
}else{
displayMessage("You Lost the game!");
}
}
});
document.querySelector('.again').addEventListener('click',function(){
secretNumber = Math.trunc(Math.random()*20)+1;
score = 20;
displayMessage("Start guessing...");
document.querySelector('.score').textContent = score;
document.querySelector('body').style.backgroundColor = '#222'
document.querySelector('.guess').value = "";
document.querySelector('.number').textContent = "?";
})