I have a simple div that should play random sounds onclick. I store my sounds inside an array of objects like this:
var sounds = [
{
animalType: 'horse',
sound: new Audio('../sounds/Horse-neigh.mp3')
},
{
animalType: 'bear',
sound: new Audio('../sounds/grizzlybear.mp3')
},
{
animalType: 'goat',
sound: new Audio('../sounds/Goat-noise.mp3'),
}
]
Then when I randomize it it gives me this error: sound.play is not a function
here is my attempt at randomizing it:
var player = document.getElementById('player');
player.addEventListener('click', function()
var sound = sounds.sort( () => Math.random() - 0.5)
sound.play()
})
Why is it giving me this error and how can I make it work? It works when it's only an array, but when using an array with objects it doesn't work.