I need to play a sound and console.log some text at the same time so I wrote a function called playAndDisplay. It plays the sound but the drum name is “undefined”. If I replace drumNames.drum
with drumNames.tink
for example it will console.log “Tink”. I think the problem is that tink is created as an object at the top of the script so the function doesn’t see it as just a string the can be added in the dot notation. But maybe I’m missing something. Is there a quick fix?
let openHat = new Audio('sounds/openhat.wav');
let tink = new Audio('sounds/tink.wav');
const drumNames = {
openHat: "Open Hihat",
tink: "Tink"
}
function playAndDisplay(drum) {
console.log(drumNames.drum);
drum.play();
}
window.addEventListener('keydown', function (event) {
var k=event.keyCode;
if(k===81) playAndDisplay(openHat); else
if(k===87) playAndDisplay(tink);
})