-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
104 lines (83 loc) · 2.12 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// get year for footer
const date = document.querySelector(".date");
date.innerHTML = new Date().getFullYear();
// Key generator
// declare the element that will display the generated key
const keyHolder = document.querySelector(".key-display");
// Then you need an array containing all of the possible options that could be passed into practiceKey
let possibleKeys = [
"C",
"F",
"Bb",
"Eb",
"Ab",
"Db",
"Gb",
"B",
"E",
"A",
"D",
"G",
"F#",
"C#",
"Cb",
];
// TODO 1 generate random number
function getRandomNumber() {
return Math.floor(Math.random() * possibleKeys.length);
}
// on click, display random element from possibleKeys array
// add an event listener to call the getRandomKey when the button is clicked and display the value
const generatorButton = document.querySelector(".key-btn");
/* generatorButton.addEventListener("click", (event) => {
let practiceKey = possibleKeys[getRandomNumber()];
keyHolder.textContent = practiceKey;
}); */
// TODO how to prevent duplicates?
// set up an array to hold used keys
const usedKeys = [];
// Apply this to the generatorButton function
generatorButton.addEventListener("click", (event) => {
if (possibleKeys.length === 0) {
possibleKeys = [
"C",
"F",
"Bb",
"Eb",
"Ab",
"Db",
"Gb",
"B",
"E",
"A",
"D",
"G",
"F#",
"C#",
"Cb",
];
}
let practiceKey = possibleKeys[getRandomNumber()];
console.log(practiceKey);
// usedKeys.unshift(practiceKey);
keyHolder.textContent = practiceKey;
// console.log(practiceKey.length);
// console.log(possibleKeys);
const index = possibleKeys.indexOf(practiceKey);
if (index > -1) {
// only splice array when item is found
possibleKeys.splice(index, 1); // 2nd parameter means remove one item only
}
console.log(possibleKeys);
});
/* write if/else statement to prevent duplicates
if (!usedKeys.includes(practiceKey)) {
usedKeys.unshift(practiceKey);
}
const newArray = possibleKeys.filter(
{
return (key) => !possibleKeys.includes(practiceKey)
});
console.log("newArray", newArray);
}
*/