-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
83 lines (78 loc) · 3.07 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
'use strict';
let snum = Math.trunc(Math.random() * 20) + 1;
let score = 20;
let highscore = 0;
document.querySelector('.check').addEventListener('click', function () {
const guess = Number(document.querySelector('.guess').value);
//Not a number
if (!guess) {
document.querySelector('.message').textContent = '😁 Type a Number';
}
//Correct answer
else if (guess === snum) {
document.querySelector('.message').textContent = '🎊 Correct Number';
document.querySelector('.number').textContent = snum;
document.querySelector('body').style.backgroundColor = '#60b347';
if (score > highscore) {
highscore = score;
document.querySelector('.high').textContent = highscore;
}
}
//Wrong answer
else if (guess !== snum) {
if (guess === snum + 2 || guess === snum + 1) {
if (score > 1) {
document.querySelector('.message').textContent = '📈 You are CLOSE';
score--;
document.querySelector('.score').textContent = score;
} else {
document.querySelector('.message').textContent = '💔 You lose';
document.querySelector('body').style.backgroundColor = '#e63946';
score = 0;
document.querySelector('.score').textContent = score;
}
} else if (guess === snum - 2 || guess === snum - 1) {
if (score > 1) {
document.querySelector('.message').textContent = '📉 You are CLOSE';
score--;
document.querySelector('.score').textContent = score;
} else {
document.querySelector('.message').textContent = '💔 You lose';
document.querySelector('body').style.backgroundColor = '#e63946';
score = 0;
document.querySelector('.score').textContent = score;
}
} else {
if (score > 1) {
let rannum = Math.trunc(Math.random() * 3) + 1;
if (rannum === 1) {
document.querySelector('.message').textContent = '😁 Keep Trying';
score--;
document.querySelector('.score').textContent = score;
} else if (rannum === 2) {
document.querySelector('.message').textContent = '😉 Dont Lose Hope';
score--;
document.querySelector('.score').textContent = score;
} else if (rannum === 3) {
document.querySelector('.message').textContent = '😄 You will get it';
score--;
document.querySelector('.score').textContent = score;
}
} else {
document.querySelector('.message').textContent = '💔 You lose';
document.querySelector('body').style.backgroundColor = '#e63946';
score = 0;
document.querySelector('.score').textContent = score;
}
}
}
});
document.querySelector('.again').addEventListener('click', function () {
score = 20;
snum = Math.trunc(Math.random() * 20) + 1;
document.querySelector('body').style.backgroundColor = '#222';
document.querySelector('.number').textContent = '?';
document.querySelector('.score').textContent = score;
document.querySelector('.guess').value = '';
document.querySelector('.message').textContent = '🧩 Start Guessing...';
});