-
Notifications
You must be signed in to change notification settings - Fork 1
/
app1.js
139 lines (126 loc) · 3.45 KB
/
app1.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
let red = document.getElementById("circle1");
let green = document.getElementById("circle2");
let blue = document.getElementById("circle3");
let purple = document.getElementById("circle4");
let circleContainer = document.querySelector(".circles");
let allCircles = document.getElementsByClassName("circleButton");
let jsScore = document.getElementById("js-score");
let jsRound = document.getElementById("js-round");
let startButton = document.getElementById("start");
let lost = document.getElementById("youLost");
let resetBtn = document.getElementById('reset')
let simon = [];
let userInput = [];
let numberOfFlashes = 1;
let score = 0;
let round = 1;
// amke 4 functions that flash the colors
const flashRed = () => {
red.style.opacity = 0.4;
setTimeout(() => {
red.style.opacity = 1;
}, 500);
};
const flashGreen = () => {
green.style.opacity = 0.4;
setTimeout(() => {
green.style.opacity = 1;
}, 500);
};
const flashBlue = () => {
blue.style.opacity = 0.4;
setTimeout(() => {
blue.style.opacity = 1;
}, 500);
};
const flashPurple = () => {
purple.style.opacity = 0.4;
setTimeout(() => {
purple.style.opacity = 1;
}, 500);
};
// randomizer, the brains of simon, give you an order that has to be followed
// randomizer has to call the flash(s) functions in that order,
const simonArray = () => {
simon.push(Math.floor(Math.random() * 4) + 1);
for (let i = 0; i < numberOfFlashes; i++) {
setTimeout(function() {
if (simon[i] == 1) {
flashRed();
} else if (simon[i] == 2) {
flashGreen();
} else if (simon[i] == 3) {
flashBlue();
} else if (simon[i] == 4) {
flashPurple();
}
}, 1000 * i);
//depending on the current round youre on make array longer
// this makes it harder for the user to repeat
}
};
const checkUserSequence = () => {
for (let i = 0; i < userInput.length; i++) {
if (userInput[i] !== simon[i]) {
console.log("in forloop for userseq");
lost.innerText = "YOU LOST";
userInput = [];
simon = [];
score = 0;
jsScore.innerText = "Score: " + score;
round = 0;
jsRound.innerText = "Round: " + round;
numberOfFlashes = 1;
hasStarted = false;
return;
}
}
moveOn();
};
const moveOn = () => {
userInput = [];
score++;
jsScore.innerText = "Score: " + score;
round++;
jsRound.innerText = "Round: " + round;
numberOfFlashes++;
startGame();
};
// a checker, that user is repeating that order(hint array comparison)
const userClicks = () => {
for (let i = 0; i < allCircles.length; i++) {
allCircles[i].addEventListener("click", function(event) {
event.target.style.opacity = 0.4;
setTimeout(() => {
event.target.style.opacity = 1;
}, 500);
userInput.push(i + 1);
if (userInput.length === simon.length) {
checkUserSequence();
}
});
}
};
const startGame = () => {
let hasStarted = true;
lost.innerText = "";
jsRound.innerText = "Round: " + round;
if (hasStarted) {
simonArray();
}
};
userClicks();
//start game
// push number in simon Array
// flash color
// get userinput
// if user matches simon Array, update score and round.
// push another number in simon array and start process again
// score counter for every round
// restart button? clears the simon array?
//
startButton.addEventListener("click", startGame);
resetBtn.addEventListener('click', function(event){
event.preventDefault();
location.reload();
})