Professional Documents
Culture Documents
EX 4 IP - PDF
EX 4 IP - PDF
EX 4 IP - PDF
No 4
Algorithm:
2. Inside the HTML file, add the necessary elements such as headings, questions, options, buttons, and result
display area.
3. Link the HTML file to a JavaScript file named "script.js" using the <script> tag.
4. In the JavaScript file, define an array of objects to store the questions, options, and correct answers.
5. Initialize variables to keep track of the current question index and the user's score.
6. Create functions to display the current question, handle the user's answer, and calculate the score.
7. Use DOM manipulation to dynamically update the HTML elements with the question, options, and result.
8. Add event listeners to handle button clicks and trigger the necessary functions.
9. Once all questions have been answered, display the final score.
Coding:
online_exam.html:
<!DOCTYPE html>
<html>
<head>
<title>Online Exam</title>
<script src="script.js"></script>
</head>
<body>
<h1>Online Exam</h1>
<div id="question"></div>
<div id="options"></div>
<div id="result"></div>
</body>
</html>
script.js:
var questions = [
answer: 1
},
answer: 0
},
{
question: "Who painted the Mona Lisa?",
answer: 0
];
var currentQuestionIndex = 0;
var score = 0;
function displayQuestion() {
questionElement.textContent = currentQuestion.question;
option.type = "radio";
option.name = "answer";
option.value = i;
optionsElement.appendChild(option);
label.textContent = currentQuestion.options[i];
optionsElement.appendChild(label);
optionsElement.appendChild(document.createElement("br"));
function handleAnswer() {
if (selectedOption) {
score++;
currentQuestionIndex++;
selectedOption.checked = false;
if (currentQuestionIndex < questions.length) {
displayQuestion();
} else {
displayResult();
function displayResult() {
document.getElementById("submit").addEventListener("click", handleAnswer);
displayQuestion();
Output: Screeshot