dom
dom

Reputation: 3

playing a randomly selected piece of audio from an array, cant figure out where I went wrong

the code is as follows:

var audio1 = new Audio("sounds/sleighride.mp3");

var audio2 = new Audio("sounds/letitsnow.mp3");


var audio3 = new Audio("sounds/comingtotown.mp3");

var array = ["audio1", "audio2", "audio3"]


function soundrandom (){ 
    var songrandom = array[Math.floor(Math.random() * array.length)]; 
    songrandom.play();
}

soundrandom();

the answer may be obvious but has had me stumped for a bit, error is "soundrandom.play(); is not a function".

Upvotes: 0

Views: 62

Answers (1)

nrylee
nrylee

Reputation: 96

They are objects not strings.

var array = [audio1, audio2, audio3]

To make it easier to edit later you could always do this:

var audioArr = [
  new Audio("sounds/sleighride.mp3"),
  new Audio("sounds/letitsnow.mp3"),
  new Audio("sounds/comingtotown.mp3")
]

Upvotes: 5

Related Questions