-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
86 lines (67 loc) · 2.58 KB
/
script.js
File metadata and controls
86 lines (67 loc) · 2.58 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
85
86
'use strict';
//variables for
let secretNumber = Math.trunc(Math.random() * 20) + 1;
let score = 20;
let highscore = 0;
const displayMessage = function (message) {
document.querySelector('.message').textContent = message;
};
const displayTextColor = function (colors) {
document.querySelector('.message').style.color = colors;
document.querySelector('.number').style.color = colors;
};
const displayBackgroundColor = function (background) {
document.querySelector('body').style.backgroundColor = background;
};
const displayScore = function (score) {
document.querySelector('.score').textContent = score;
}
//use .textContent to select the text from the element. some elements have lots of other info so if you are just changing the text, use .textcontent
//when check is clicked this gets the value
document.querySelector('.check').addEventListener('click', function () {
const guess = Number(document.querySelector('.guess').value);
//when no number is entered
if (!guess) {
displayMessage('No number!');
// when correct number is entered
} else if (guess === secretNumber) {
displayMessage('You are correct!');
displayTextColor('#60b347');
displayBackgroundColor('#60b347');
document.querySelector('.number').textContent = secretNumber;
confetti({
particleCount: 100,
spread: 70,
origin: {
y: 0.6
}
});
if (score > highscore) {
highscore = score;
document.querySelector('.highscore').textContent = highscore;
}
//when number is wrong
} else if (guess !== secretNumber) {
if (score > 1) {
guess > secretNumber ? displayMessage('Too high! Try a smaller number.') : displayMessage('Too low! Try a bigger number.');
score--;
displayScore(score);
displayTextColor('#990000');
} else {
displayMessage('You lost the game, but you can try again!');
displayScore(0);
displayTextColor('#990000');
}
}
//play again reset page
document.querySelector('.again').addEventListener('click', function () {
score = 20;
secretNumber = Math.trunc(Math.random() * 20) + 1;
displayMessage('Guess the number I\'\m thinking of...');
document.querySelector('.number').textContent = '?';
document.querySelector('.guess').value = '';
displayTextColor('#000000');
displayBackgroundColor('#ffffff');
displayScore(score);
});
});