/    Sign up×
Community /Pin to ProfileBookmark

I got this small html quiz project that I would want to be able to play distinct sound whether the user gets the answer wrong or right. I also wanted to make it play distinct sounds according to total percent of the result. If you’re interested for the history of the project here is the[URL=”http://www.webdeveloper.com/forum/showthread.php?304231-HTML-Random-Quiz-Game-w-Timer”] link.[/URL]

Here’s the quiz

[CODE]
<!DOCTYPE html>
<head>
<title>Random Quiz</title>
<style>
div#test{
padding:10px 40px 40px 40px;
background-image: url(“.jpg”);
no-repeat center center fixed;
background-size: cover;
}
</style>
</head>
<body>
<div>
<h1 id=”timeleft”></h1>
<h2 id=”test_status”></h2>
</div>
<div id=”test”></div>

<script>
var myVar;
function startTimer() {
myVar = setInterval(function(){myTimer()},1000);
timelimit = maxtimelimit;
}
function myTimer() {
if (timelimit > 0) {
curmin=Math.floor(timelimit/60);
cursec=timelimit%60;
if (curmin!=0) { curtime=curmin +cursec; }
else { curtime=cursec; }
$_(‘timeleft’).innerHTML = “00:0″+curtime;
} else {
//$_(‘timeleft’).innerHTML = timelimit+’ – Out of Time’;
//clearInterval(myVar);
checkAnswer();
}
timelimit–;
}

var pos = 0, posn, choice, correct = 0, rscore = 0;
var maxtimelimit = 6, timelimit = maxtimelimit; // 20 seconds per question

var questions = [
[ “What is 8+6?”, “12”, “14”, “16”, “B” ],
[ “What is 20 – 9?”, “7”, “13”, “11”, “C” ],
[ “What is 7 x 3?”, “21”, “24”, “25”, “A” ],
[ “What is 8 / 2?”, “10”, “2”, “4”, “C” ],
[ “What is 8 ^ 2?”, “8”, “2”, “64”, “C” ],
[ “What is 8 mod 2?”, “0”, “1”, “4”, “A” ],
[ “What is 6 + 4 + 2?”, “12”, “14”, “16”, “A” ],
[ “What is 20 – 7?”, “7”, “13”, “11”, “B” ],
[ “What is 5 x 5?”, “21”, “24”, “25”, “C” ],
[ “What is 8 / 4?”, “10”, “2”, “4”, “B” ]
];
var questionOrder = [];
function setQuestionOrder() {
questionOrder.length = 0;
for (var i=0; i<questions.length; i++) { questionOrder.push(i); }
questionOrder.sort(randOrd); // alert(questionOrder); // shuffle display order
pos = 0; posn = questionOrder[pos];
}

function $_(IDS) { return document.getElementById(IDS); }
function randOrd() { return (Math.round(Math.random())-0.5); }

function renderResults(){
var test = $_(“test”);
test.innerHTML = “<h2>You got “+correct+” of “+questions.length+” questions correct</h2>”;
$_(“test_status”).innerHTML = “Test Completed”;
$_(‘timeleft’).innerHTML = ”;
test.innerHTML += ‘<button onclick=”location.reload()”>Re-test</button>’;
setQuestionOrder();
correct = 0;
clearInterval(myVar);
return false;
}
function renderQuestion() {
var test = $_(“test”);
$_(“test_status”).innerHTML = “Question “+(pos+1)+” of “+questions.length;
//if (rscore != 0) { $_(“test_status”).innerHTML += ‘<br>Currently: ‘+(correct/rscore*100).toFixed(0)+’% correct’; }
var question = questions[posn][0];
var chA = questions[posn][1];
var chB = questions[posn][2];
var chC = questions[posn][3];
test.innerHTML = “<h2>”+question+”</h2>”;
test.innerHTML += “<label><input type=’radio’ name=’choices’ value=’A’> “+chA+”</label>”;
test.innerHTML += “<label><input type=’radio’ name=’choices’ value=’B’> “+chB+”</label>”;
test.innerHTML += “<label><input type=’radio’ name=’choices’ value=’C’> “+chC+”</label>”;
test.innerHTML += “<br/><button onclick=’checkAnswer()’>Submit Answer</button>”;
timelimit = maxtimelimit;
clearInterval(myVar);
startTimer();
}

function checkAnswer(){
var choices = document.getElementsByName(“choices”);
for (var i=0; i<choices.length; i++) {
if (choices[i].checked) { choice = choices[i].value; }
}
rscore++;
if (choice == questions[posn][4] && timelimit > 0) { correct++; }
pos++; posn = questionOrder[pos];
if (pos < questions.length) { renderQuestion(); } else { renderResults(); }
}

window.onload = function() {
setQuestionOrder();
renderQuestion(pos);
}
</script>

</body>
</html>
[/CODE]

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@junozmuauthorFeb 14.2015 — Someone help me please.
Copy linkTweet thisAlerts:
@jedaisoulFeb 14.2015 — I do not know the answer to your query, but I just googled "javascript sound", and it came up with [url=http://www.lmgtfy.com?q=javascript+sound]these results[/url]. I hope that helps... ?
Copy linkTweet thisAlerts:
@junozmuauthorFeb 17.2015 — It's not just about calling a sound to play its about the conditions being met before calling a function to play a sound.
×

Success!

Help @junozmu spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.15,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...