Passing parameter to function (drum machine)

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);
  
})

Check out the explanation concerning bracket notation in the following article, especially where it covers passing in a variable for a property.

Just as an aside, I don’t think using the Audio constructor is going to work for the tests. The test will try to call play on the audio element in the DOM using the buttons and then look at the audio elements .paused property to see if it is playing or not.

You’re right! Got ahead of myself.