Create a website that displays an adding quiz (use random numbers between 0 and
ID: 3574622 • Letter: C
Question
Create a website that displays an adding quiz (use random numbers between 0 and 100 - see the image below). The elapsed time will also displayed. When an answer is submitted, your JS program should check whether the given answer is correct or incorrect, and then a suitable pop-up message- whether the answer is wrong or right- should be displayed. The timer should wait until the user click the okay button. If the answer is correct a new quiz should be displayed, and the timer should be initialized to 0. If the answer is wrong, the original quiz should be displayed and the answer box should be reset. The timed should continue from the previous value.Explanation / Answer
<html>
<head>
<script>
var time=-1;
var timer;
function generateQuiz()
{
document.getElementById("val1").innerHTML = getRandomNumber();
document.getElementById("val2").innerHTML = getRandomNumber();
time=-1;
clearTimeout(timer);
timer=setTimeout(startTimer, 1000);
}
function startTimer()
{
time=+time+1;
document.getElementById("time").innerHTML=time;
timer=setTimeout(startTimer, 1000);
}
function getRandomNumber()
{
return Math.floor((Math.random() * 100) + 0);
}
function validateQuiz()
{
val1=document.getElementById('val1').innerHTML;
val2=document.getElementById('val2').innerHTML;
var answer=document.getElementById('answer').value;
if(answer.trim()!="")
{
var actualAnswer = +val1 + +val2;
if(actualAnswer==answer.trim())
{
alert("Right Answer");
generateQuiz();
document.getElementById("answer").value ="";
}
else
{
alert("Answer is wrong, Try again");
document.getElementById("answer").value ="";
}
}
else
{
alert("Enter your answer");
}
}
function validate(evt) {
var theEvent = evt || window.event;
var key = theEvent.keyCode || theEvent.which;
key = String.fromCharCode( key );
var regex = /[0-9]|./;
if( !regex.test(key) ) {
theEvent.returnValue = false;
if(theEvent.preventDefault) theEvent.preventDefault();
}
}
</script>
</head>
<body>
<h1>Adding Quiz</h1>
<p>What is <span id="val1"></span>+<span id="val2"></span></p>
<input type="text" name="answer" id="answer" />
<input type="button" name="ok" value="ok" />
<p>Time spent on this question so far: <span id="time"> </span></p>
</body>
</html>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.