I would like to avoid writing lots of code for a big list of items. For example, I have this:
let f_a = document.querySelector('#FToneA');
let f_b = document.querySelector('#FToneB');
let f_c = document.querySelector('#FToneC');
// etc...
let fNameList = [f_a, f_b, f_c,]; // etc...
let fToneA = new Audio('../instrumentarium/assets/snd/flute/A_F.wav');
let fToneB = new Audio('../instrumentarium/assets/snd/flute/B_F.wav');
let fToneC = new Audio('../instrumentarium/assets/snd/flute/C_F.wav');
// etc...
let fToneList = [fToneA, fToneB, fToneC,]; // etc...
And then I have the events that should happen with each of the elements, that is a .wav file plays when you mouse over an element.
for (var t of fNameList) {
t.addEventListener('mouseover', function() {
function tSet(tone) {
tone.play();
};
if (this === f_a) {
tSet(fToneA);
} else if (this === f_b) {
tSet(fToneB);
} else if (this === f_c) {
tSet(fToneC);
} // etc...
And what I want to do is similar to what you would do in Python with list.index(), so what I tried doing instead of the above is the following:
for (var nameIndex of fNameList) {
nameIndex.addEventListener('mouseover', function() {
for (var toneIndex of fToneList) {
if (fNameList.indexOf(nameIndex) === fToneList.indexOf(toneIndex)) {
toneIndex.play();
}
}
})
};
In other words, in Python it would be just index() instead of indexOf(). But I suppose I'm doing something wrong here. Most likely at the line above "toneIndex.play()". Because what happens is only one sound plays for every element I hover/click.
Can anyone help me in writing this properly?