var buttonColours = ["red", "blue", "green", "yellow"];
var gamePattern = [];
function nextSequence() {
var randomNumber = Math.floor(Math.random() * 4);
var randomChosenColour = buttonColours[randomNumber];
gamePattern.push(randomChosenColour);
$("#"+randomChosenColour).fadeIn(100).fadeOut(100).fadeIn(100);
var audio = new Audio("sounds/"+randomChosenColour+".mp3");
audio.play().catch(function(error) {
console.log("Error occurred: " + error.message);
// Add a play button for user interaction with the document
});
}
nextSequence();
The play() function is not working and it is giving the error "Uncaught (in promise) DOMException: play() failed because the user didn't interact with the document first."
Suggest some fix.