-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathletters-script.js
130 lines (103 loc) · 3.94 KB
/
letters-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
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
//Covers the numbers so they can't be clicked after selecting correct answer
var cover = document.querySelector('#cover');
//Variables for storing numbers
var randomNumber;
var usedArray = [];
var lettersArray = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "sh", "ch", "th", "ing"];
//Variables for audio
var audioEl = [];
var audioBoom;
var audiocongrats;
audioBoom = document.createElement('audio');
audioBoom.src="audio/correct.mp3";
//Create all the audio elements
for (a=0; a < lettersArray.length; a++) {
audioEl[a] = document.createElement('audio');
audioEl[a].src=`audio/${lettersArray[a]}.mp3`;
}
//Getting a new random number
var btn = document.querySelector("#startBtn");
btn.addEventListener("click", getLetter);
function getLetter() {
//Styling to make sure the cover is gone and REPLAY button shows
cover.style.display = "none";
replay.style.display = "inline-block";
//Create random number and remove click event so it won't create another number yet
randomNumber = Math.floor(Math.random() * 30);
btn.removeEventListener("click", getLetter);
//Conditional logic to be make sure it gets a number that hasn't been used yet
if (!usedArray.includes(randomNumber)) {
audioEl[randomNumber].play();
usedArray.push(randomNumber);
//audioEl[randomNumber].addEventListener("ended", chooseLetter);
chooseLetter();
} else {
getLetter();
}
}
//Variables for all the numbers, count correct answers, and for the unicorns
var counter = 0;
var numbers = document.querySelectorAll(".numLets");
var unicorns = document.querySelectorAll('.unicorns');
function chooseLetter() {
//Get the number that has been clicked
var chosen = this.dataset.number;
var chosenEl = document.getElementById(this.id);
//If the clicked number is NOT the same as the random number then play the number's audio
if (randomNumber !== chosen) {
audioEl[randomNumber].play();
}
//If the clicked number and random number are the same and the counter is less than 10, then do stuff
if (randomNumber == chosen && counter < 5) {
chosenEl.style.backgroundColor = "#F3A3E1";
audioEl[randomNumber].pause();
audioBoom.play();
cover.style.display = "block";
btn.addEventListener("click", getLetter);
replay.style.display = "none";
unicorns[counter].style.opacity = 1;
counter++;
if(counter == 5) {
playAgainBtn.style.display="block";
congrats.style.display="block";
gameOver.style.display="block";
unicorn_win.style.display = "block";
audioBoom.pause();
audiocongrats = document.createElement('audio');
audiocongrats.src = "audio/congratulations.mp3";
audiocongrats.play();
}
}
}
for (n of numbers) {
n.addEventListener("click", chooseLetter);
}
//Play the random number audio again in case player forgot or needs to hear it again
var replay = document.querySelector('#replay');
replay.addEventListener("click", replayAudio);
function replayAudio() {
audioEl[randomNumber].play();
}
//Play the game again. Reset all the variables back to the start state
var playAgainBtn = document.querySelector('#playagain');
var gameOver =document.querySelector('#gameover');
var congrats =document.querySelector('#congrats');
var unicorn_win = document.querySelector('#unicorn-win');
playAgainBtn.addEventListener("click", playagain);
function playagain() {
playAgainBtn.style.display="none";
congrats.style.display="none";
gameOver.style.display="none";
unicorn_win.style.display="none";
counter=0;
for (u of unicorns) {
u.style.opacity=0;
}
for (n of numbers) {
n.style.backgroundColor="white";
}
usedArray = [];
console.log("Used array is " + usedArray);
console.log("counter is " + counter);
console.log("random number is " + randomNumber);
}