What I'm trying to do is simply to pick an audio file at random and play it. I'm trying to use the built-in .play() function.
I have four audio clips defined, sound0 - sound3, for example:
<audio id="sound0" src="https://s3.amazonaws.com/freecodecamp/simonSound1.mp3" type="audio/mpeg"></audio>
In JS I can store a pointer to that object like so:
$sound0 = $("#sound0")[0];
and I can play it directly with
$sound0.play();
However, if I try to do something like the following,
var pattern = [],
tone;
pattern.push(Math.floor(Math.random() * 4));
tone = "#sound" + pattern[0];
$(tone).play();
I get the error, Uncaught TypeError: $(...).play is not a function.
What is the best way to approach this? Thank you!