forked from wesbos/JavaScript30
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjavaScript.js
More file actions
49 lines (41 loc) · 1.13 KB
/
javaScript.js
File metadata and controls
49 lines (41 loc) · 1.13 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
const keys = [
"65",
"83",
"68",
"70",
"71",
"72",
"74",
"75",
"76"
];
let score = 0 ;
function sound(e){
console.log(e.keyCode);
const audio = document.querySelector(`audio[data-key="${e.keyCode}"]`);
const button = document.querySelector(`div[data-key="${e.keyCode}"]`);
if(!audio) return;
if(button.classList.contains('click')){
document.getElementById('score').innerHTML= ++score;
}
button.classList.add('playing');
audio.currentTime=0;
audio.play();
}
function effect(button){
button.addEventListener('transitionend', e =>
{
if(e.propertyName==="transform")
button.classList.remove('playing');
button.classList.remove('click');
}
)}
function randomKey(preKey=65){
const random = Math.floor((Math.random())*9);
const key = keys.filter((_,index) => random == index);
document.querySelector(`div[data-key="${key}"]`).classList.add('click');
setTimeout(_=>randomKey(key),1000);
}
window.addEventListener('keydown', sound);
document.querySelectorAll('.key').forEach(effect);
setTimeout(randomKey,1000);