-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
90 lines (76 loc) · 3.28 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
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
85
86
87
88
89
90
'use strict';
/* *QUERY SELECTOR*
console.log(document.querySelector('.message').textContent);
document.querySelector('.message').textContent = "Correct Number";
document.querySelector('.number').textContent = "13";
document.querySelector('.score').textContent = "10";
document.querySelector('.guess').value = 23;
console.log(document.querySelector('.quess').value);
*/
var secretNumber = Math.trunc(Math.random() * 20) + 1;
var score = 20;
var highScore = 0;
var displayMessage = function (message){
document.querySelector('.message').textContent = message;
};
document.querySelector('.check').addEventListener('click', function(){
const guessed = Number(document.querySelector('.guess').value);
//When there is no Input
if (!guessed){
displayMessage("No number");
document.querySelector('.score').value = score;
//When the player wins
}else if (guessed == secretNumber){
displayMessage("Yes, that's correct!");
document.querySelector('body').style.backgroundColor = "Green"
document.querySelector('.number').style.width = '30rem'
document.querySelector('.number').textContent = secretNumber;
if (score > highScore){
highScore = score
document.querySelector('.highscore').textContent = highScore;
}
// DRY PRINCIPLE
// When the score is not equal to secret number
}else if (guessed != secretNumber){
if (score > 1){
document.querySelector('.message').textContent = guessed > secretNumber ? "Too high" : "Too low";
score--;
document.querySelector('.score').textContent = score;
}else{
displayMessage("You lost the game")
document.querySelector('.score').textContent = 0;
}
}
// *************NORMAL CODE WITHOUR 'DRY PRINCIPLE'************
// else if (guessed > secretNumber){
// if (score > 1){
// document.querySelector('.message').textContent = "Too high!";
// score--;
// document.querySelector('.score').textContent = score;
// }else{
// document.querySelector('.message').textContent = "You lost the game";
// document.querySelector('.score').textContent = 0;
// }
// // When the score is too low
// }else if (guessed < secretNumber){
// if (score > 1){
// document.querySelector('.message').textContent = "Too low!";
// score--;
// document.querySelector('.score').textContent = score;
// }else{
// document.querySelector('.message').textContent = "You lost the game";
// document.querySelector('.score').textContent = 0;
// }
// }
});
// AFTER CLICKING THE RESET BUTTON
document.querySelector('.again').addEventListener('click', function(){
secretNumber = Math.trunc(Math.random() * 20) + 1;
score = 20;
document.querySelector('body').style.backgroundColor = "Black";
document.querySelector('.number').style.width = "15rem";
document.querySelector('.message').textContent = "start guessing...";
document.querySelector('.score').textContent = score;
document.querySelector('.number').textContent = '?';
document.querySelector('.guess').value = '';
});