exam with timer in javascript [closed]
I would appreciate it if you can help me with this program. I would like the program to run ike this:
- upon clicking a button, a question like 3 X 2 will be written in the document with a textbox wherein I can put the answer.
- check if the answer is right or wrong using a promptbox.
- It should have a timer that goes from 60 to 0 and when it reaches zero and you weren't able to answer it, then it will prompt you that the time is up.
I figured out the countdown timer already. I just don't know how to create the rest ^_^.
it would be great if you can incorporate it with my codes that i have figured. here is my code so far..
<html>
<head>
<title>JavaScript Timer</title>
<script type="text/javascript">
var SECONDS_LEFT, TIMER, TIMES_UP;
function resetTimer(seconds){
SECONDS_LEFT = seconds;
document.Timer.TimeLeft.value = SECONDS_LEFT;
clearTimeout(TIMER);
}
function decrementTimer(){
TIMES_UP = false;
document.Timer.TimeLeft.value = SECONDS_LEFT;
SECONDS_LEFT--;
if (SECONDS_LEFT >= 0) {
TIMER = setTimeout(decrementTimer, 1000);
} else {
alert("Time's up!");
resetTimer(60);
}
}
</script>
</head>
<body onload="resetTimer(60)">
<form name="Timer" onsubmit="return false;">
Timer: <input type="text" name="TimeLeft" size="2"
style=开发者_StackOverflow中文版"text-align:center" onfocus="this.blur();">
seconds left<br>
<input type="button" name="btnStart"
value="Start Quizz" onclick="decrementTimer();">
<input type="button" name="btnReset"
value="Retry" onclick="resetTimer(60);">
</form>
</body>
</html>
you could store the question and answers in a multidimensional array like follows
// [question number, answer, question]
var qanda =[
[1,15,"Add 11 + 4"],
[2,7,"Add 4 + 3"],
[3,7,"Subtract 9 - 2"]
];
function getQuestion(n){
document.getElementById("disp_question").innerHTML = qanda[n][2];
document.getElementById("intp_div").style.display = "block";
}
function checkAnswer(n){
if(document.getElementById("intp_answer").value != undefined){
if(document.getElementById("intp_answer").value == qanda[n][1]){
alert("correct")
}else{
alert("incorrect")
}
}
}
And you could add this below your retry button
<div id="intp_div" style="display:none">
<h3 id="disp_question"></h3><input id="intp_answer" type="text" value=""><input type="button" value="Submit Answer" onclick="checkAnswer(2)" />
</div>`
this it just to hepl you get on your way Note:
onclick="checkAnswer(2)"
the "2" is the question your refferencing from the q and a array
精彩评论