-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
80 lines (73 loc) · 2.19 KB
/
script.js
File metadata and controls
80 lines (73 loc) · 2.19 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
const colorcode=document.getElementById("color-code");
const option=document.getElementById("option");
const scoreContain=document.getElementById("score");
const op=document.getElementById('op');
const btn=document.getElementById('btn');
const correctMessages = [
"Correct! 🎉",
"Nice pick!😃",
"You nailed it!👍🏻",
"Perfect match!👌",
"Great job! 🎯"
];
const wrongMessages = [
"Wrong one 🙃",
"Oops! Try again.😶",
"Not quite...🤔",
"That’s not it! 😏",
"Missed it!🙄"
];
let randomcolor=null;
let score=0;
function generateRandomNumber(min, max) {
return min + Math.floor(Math.random() * (max - min + 1));
}
function genrateRandomColorRGB() {
const red=generateRandomNumber(0,255);
const green=generateRandomNumber(0,255);
const blue=generateRandomNumber(0,255);
return `rgb(${red}, ${green}, ${blue})`;
}
function incrementStore(){
score+=1;
scoreContain.innerText=score;
}
function validateResult(el){
console.log(el.target);
const selectedColor=el.target.style.backgroundColor;
if((randomcolor===selectedColor)){
incrementStore();
const msg = correctMessages[Math.floor(Math.random() * correctMessages.length)];
op.innerText = msg;
}
else{
const msg = wrongMessages[Math.floor(Math.random() * wrongMessages.length)];
op.innerText = msg;
}
localStorage.setItem('score',score)
startGame() ;
}
function startGame(){
score=Number(window.localStorage.getItem('score')) || 0;
scoreContain.innerText=score;
option.innerHTML=null;
randomcolor=genrateRandomColorRGB();
colorcode.innerText=randomcolor;
const ansindex=generateRandomNumber(0,5);
for(let i=0;i<6;i++){
const div=document.createElement('div');
div.addEventListener('click',validateResult);
div.style.backgroundColor = i === ansindex ? randomcolor :genrateRandomColorRGB();
option.append(div);
}
}
window.addEventListener('load',startGame());
window.addEventListener('beforeunload', () => {
localStorage.removeItem('score');
});
btn.addEventListener('click', () => {
localStorage.removeItem('score');
score = 0;
scoreContain.innerText = score;
startGame();
});