Click here to Skip to main content
15,355,296 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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.
Posted
Comments
Richard Deeming 10-Dec-21 3:51am
   
There's a secret error somewhere in your secret code. You need to fix that.

Seriously, how do you expect anyone to help you fix code we can't see, when the only details we have is that it's "not working"?

Click the green "Improve question" link. Add the relevant parts of your code, and a complete description of the problem.
Gerry Schmitz 10-Dec-21 16:41pm
   
"mouse over" fires continuously ... probably flooding any play attempts. Start with a "click" (and play), and go from there. (Or maybe on mouse enter)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900