I made a quiz using javascript and my options and questions are in an array. How can I put a sound on an option everytime the user hover them?
this is my javascript code:
var images = {
"water" : "water.png",
"rainbow" : "bell.png",
"fireworks" : "fireworks.png",
}
var sounds = {
"water" : "bell.mp4",
"rainbow" : "rainbow.mp4",
"fireworks" : "fireworks.mp4",
}
const quiz = [
{
q: "Click on the picture of the fireworks",
options:['water','bell','fireworks'],
answer:2
},{
q: "Click on the picture of water",
options:['water','bell','fireworks'],
answer:0
},{
q: "Click on the picture of bell",
options:['water','bell','fireworks'],
answer:1
],
let questionCounter = 0;
let currentQuestion;
let availableQuestions = [];
let availableOptions =[];
let correctAnswers = 0;
let attempt = 0;
function setAvailableQuestion(){
const totalQuestions = quiz.length;
for(let i=0; i<totalQuestions;i++){
availableQuestions.push(quiz[i])
}
}
function getnewQuestion(){
const questionIndex = availableQuestions[Math.floor(Math.random()*availableQuestions.length)];
currentQuestion = questionIndex;
questionText.innerHTML = currentQuestion.q;
const index1= availableQuestions.indexOf(questionIndex);
availableQuestions.splice(index1,1);
const optionLen = currentQuestion.options.length
for(let i=0; i<optionLen; i++){
availableOptions.push(i)
}
optionContainer.innerHTML = '';
for (let i=0; i<optionLen; i++){
const optionIndex = availableOptions[Math.floor(Math.random()*availableOptions.length)];
const index2 = availableOptions.indexOf(optionIndex);
availableOptions.splice(index2,1);
console.log(optionIndex)
const option = document.createElement("div");
option.innerHTML = images[currentQuestion.options[optionIndex]]? '<img src="'+images[currentQuestion.options[optionIndex]]+'"/>':currentQuestion.options[optionIndex];
option.id =optionIndex;
option.className = "option";
optionContainer.appendChild(option);
option.setAttribute("onclick","getResult(this)");
}
questionCounter++;
}
What I have tried:
I kept trying on using an onmouseover event and assigning a variable for the sounds but it seems to be not working.