-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
83 lines (71 loc) · 1.86 KB
/
app.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
const rgb = document.getElementById('rgb');
const options= document.getElementById('optionContainer');
const scoreContainer = document.getElementById("span");
const spann = document.getElementById("span");
const resett = document.getElementById("reset");
let score=0;
let result=null;
function randomNumber(min,max){
return min+ Math.floor(Math.random()*(max-min)+1);
}
function randomrgb(){
const r= randomNumber(0,255);
const g= randomNumber(0,255);
const b= randomNumber(0,255);
return `rgb(${r}, ${g}, ${b})`;
}
function scoreincre(){
score+=1;
console.log(score);
spann.innerHTML=score;
}
function validateResult(el) {
const res=el.target.style.backgroundColor;
console.log(result);
console.log(res);
console.log(res===result)
if(res===result){
scoreincre();
}
else{
score=0;
}
window.localStorage.setItem('score',score);
startgame();
}
function startgame( ){
score= Number(window.localStorage.getItem('score')?? 0);
spann.innerText=score;
options.innerHTML=null;
result = randomrgb();
rgb.innerHTML= result;
const mynum=randomNumber(0,4);
console.log(mynum);
for(let i =1; i<=4; i++){
const div = document.createElement('div');
// console.log(randomrgb());
if(i===mynum){
div.style.backgroundColor=result;
}
else
{
div.style.backgroundColor=randomrgb();
}
div.addEventListener('click',validateResult)
options.append(div);
// console.log(div);
}
}
function resetfun(){
if( confirm("are u sure to reset ")){
window.localStorage.setItem('score',"0");
score=0;
spann.innerText=0;
// document.write("reseted")
console.log("reseted")}
else {
console.log("not reseted")
}
}
window.addEventListener("load",startgame());
// resett.addEventListener('click',resetfun);