-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
120 lines (108 loc) · 3.25 KB
/
index.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
//Global varibales
const gameBox = document.getElementById('game-box')
const messageModal = document.getElementById('message-modal')
const gameTriangle = document.querySelectorAll('.game-triangle')
const modalMessage = document.getElementById('modal-message')
const modal = document.getElementById('message-modal')
const start = document.getElementById('start-btn')
let voiceAnswersArr = []
let score = 0
window.addEventListener('load', event => {
const loadValue = event.target.all[11].value
modalMenu()
})
//Modal menu
function modalMenu() {
start.addEventListener('click', startTheGame, true)
modal.style.display = 'block'
}
function startTheGame() {
modal.style.display = 'none'
startGame()
modalMessage.innerText = ''
}
//Game
function startGame() {
start.removeEventListener('click', startTheGame, true)
let gameArr = []
let playerArr = []
gameTriangle.forEach(tri => {
gameArr.push(Math.floor(Math.random() * gameTriangle.length ) + 1)
})
let i = 0
function gameLoop() {
document.getElementById(`btn-${gameArr[i]}`).style.opacity = 1
setTimeout(() => {
document.getElementById(`btn-${gameArr[i]}`).style.opacity = .4
i++
}, 400)
if (i < (gameArr.length - 1)) {
setTimeout( gameLoop, 1000)
}
}
gameLoop()
function getVoiceArr() {
window.SpeechRecognition = window.SpeechRecognition || webkitSpeechRecognition;
const answer = new SpeechRecognition()
answer.addEventListener('result', event => {
let voiceAnswers = Array.from(event.results)
.map(result => result[0])
.map(result => result.transcript)
let voiceAnswersArr = voiceAnswers[0]
voiceAnswersArr = voiceAnswersArr.split(' ')
if (voiceAnswersArr.length >= gameArr.length) {
for (const color of voiceAnswersArr) {
if (color === 'yellow'){
playerArr.push(1)
} else if (color === 'red') {
playerArr.push(2)
} else if (color === 'blue') {
playerArr.push(3)
} else {
modalMessage.innerText = 'I\'m sorry I didn\'t understand that.'
modalMenu()
}
}
if (playerArr.length > 0 && playerArr.length === gameArr.length) {
checkForWinner()
}
}
})
answer.start();
}
getVoiceArr()
gameTriangle.forEach(tri => {
tri.addEventListener('click', event => {
const getTriId = event.target.id
const getNum = getTriId.split('')
document.getElementById(getTriId).style.opacity = 1
setTimeout(() => {
document.getElementById(getTriId).style.opacity = .4
playerArr.push(parseInt(getNum[getNum.length - 1]))
if (playerArr.length > 0 && playerArr.length === gameArr.length) {
checkForWinner()
}
}, 400)
})
});
function checkForWinner() {
let score = 0
for (let i = 0; i < playerArr.length; i++) {
if (playerArr[i] === gameArr[i]) {
score++
}
if (playerArr.length === i + 1) {
checkScore(score)
}
}
function checkScore(s) {
if (s === playerArr.length) {
modalMessage.innerText = 'You Win'
modalMenu()
} else {
modalMessage.innerText = 'Oh No, Try Again'
modalMenu()
}
}
}
}