Python Quiz
Test your knowledge of Python with this quiz!
<script src="https://pytutorials.github.io/quiz.js"></script>
<h1>Python Quiz</h1>
<p>Test your knowledge of Python with this quiz!</p>
<div class="container">
<div class="question-form">
<h1>Question 1:</h1>
<p>What is the output of this code?</p>
<p>print(2+2+2)</p>
<form id="answer" value="">
<input type="checkbox" value="1">8</input>
<input type="checkbox" value="2">4</input>
<input type="checkbox" value="3">2</input>
<input type="checkbox" value="4">6</input>
<button id="submit" onclick="check_answer()">Submit Answer (this button may not work yet :)</button>
</form>
</div>
</div>
type here
function check_answer(){
var ans = document.getElementById("answer");
if(ans.value != 4){
var h1 = document.createElement("h1");
h1.innerText = "Try Again!";
document.body.appendChild(h1);
}
if(ans.value == 4){
var h1 = document.createElement("h1");
h1.innerText = "Correct!";
document.body.appendChild(h1);
}
}
-
This html code is used to create the form, etc.
The result is a form with 4 check boxes that say, “8”, “4”, “2”, “6”. Then there is a button that says, “submit answer” overall this is just the first question.
The JavaScript code checks if the user has clicked the right answer and that is it. -
Thanks for the help!
3
Answers
the
ans.value is undefined
this is why you see "Try again" every time.This code works (you have to add a
checkbox
class to your checkboxes input):}
But careful, in this situation, you have to use a radio buttons and not checkboxes because there is only one correct answer.
</input>
closing tags so I removed that as well from your HTMLcorrect
ortry again
message)form
(which doesn’t have that attribute)<h1>
and change the textContent of that element, depending on selection, clear that element with each button press before assigning it a new contentIn this case it is better to use radio buttons. Skip all the IDs and just refer to the form using the name (and the same goes for input elements).