Quiz Maker Using JavaScript

Home » Coding » Quiz Maker Using JavaScript

Introduction of the Project

Quizzes are a great way to test one’s knowledge and keep the mind engaged. With the rise of the internet, online quizzes have become increasingly popular. Creating a quiz from scratch can be a time-consuming and complex task. However, with the help of JavaScript, one can create a quiz easily and efficiently. In this JavaScript tutorial, we will learn how to build a quiz maker using JavaScript.

We will cover everything from setting up the HTML and CSS to writing the JavaScript code that powers the quiz. By the end of this coding tutorial, you will have a functioning quiz that you can customize to your needs. So, whether you’re a trained developer or just starting out, let’s dive in and start creating your very own quiz maker using JavaScript!

 

Objectives

1. To match the user’s selected answer with the right answer, and if it matches, increment the score by 2 and display the total score at the end.

2. Understanding the basics of HTML, CSS, and JavaScript.

3. Setting up the HTML structure for the quiz, including the questions, answers, and results.

4. Styling the quiz using CSS to make it visually appealing and user-friendly.

5. Writing the JavaScript code to handle quiz logic, including displaying questions, checking answers, and displaying results.

6. Implementing a scoring system to keep track of the user’s progress.

7. Adding interactivity to the quiz, such as allowing the user to select an answer, providing feedback on incorrect answers, and displaying the final score.

8. Enhancing the quiz with additional features, such as a timer, multiple choice questions, and randomizing the order of questions.

9. Testing the quiz to ensure it is functioning correctly.

Requirements

Here are the specific requirements to build a Quiz Maker Using JavaScript:

1. Visual Studio Code or any other text editor to write the HTML, CSS, and JavaScript code.

2. Basic understanding of JavaScript, HTML, and CSS.

3. Web Browser: To run the quiz and view the results.

4. Web server (such as XAMPP or WAMP): To host the quiz if you want to run it on a local machine.

5. Knowledge of JavaScript concepts such as variables, arrays, loops, and functions.

6. Understanding of event handling and how to use JavaScript to interact with HTML elements.

7. Familiarity with DOM (Document Object Model) manipulation, which is the process of changing or modifying the content of a web page using JavaScript.

Source Code

HTML Code

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<title>Document</title>
</head>
<body>
<div class="quiz-container" id="quiz">
<div class="quiz-header">
<h2 id="question">Question Text</h2>
<ul>
<li>
<input type="radio" name="answer" id="a" class="answer">
<label for="a" id="a_text">Answer</label>
</li>
<li>
<input type="radio" name="answer" id="b" class="answer">
<label for="b" id="b_text">Answer</label>
</li>
<li>
<input type="radio" name="answer" id="c" class="answer">
<label for="c" id="c_text">Answer</label>
</li>
<li>
<input type="radio" name="answer" id="d" class="answer">
<label for="d" id="d_text">Answer</label>
</li>
</ul>
</div>
<button id="submit" onclick="submitAns()">Submit</button>
</div>
<script src="quiz.js"></script>
</body>
</html>

CSS Code

body{
background-color: #b8c6db;
background-image: linear-gradient(315deg, #b8c6db 0%, #f5f7f7 100%);
font-family: 'Poppins', sans-serif;
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
overflow: hidden;
margin: 0;
}
.quiz-container{
background-color: #fff;
border-radius: 10px;
box-shadow: 0 0 10px 2px rgba(100, 100, 100, 0.1);
width: 600px;
overflow: hidden;
}
.quiz-header{
padding: 4rem;
}
h2{
padding: 1rem;
text-align: center;
margin: 0;
}
ul{
list-style-type: none;
padding: 0;
}
ul li{
font-size: 1.2rem;
margin: 1rem 0;
}
ul li label{
cursor: pointer;
}
button{
background-color: #03cae4;
color: #fff;
border: none;
display: block;
width: 100%;
cursor: pointer;
font-size: 1.1rem;
font-family: inherit;
padding: 1.3rem;
}
button:hover{
background-color: #04adc4;
}
button:focus{
outline: none;
background-color: #44b927;
}

JAVASCRIPT Code

const ques = ["Amoeba moves with the help of?","Digestion in human takes place in?","Formula of Sodium Chloride is?"];
const ans = ["a","c","b"];
const options = [["Pseudopodia","Cilia","Legs","None"],["Brain","Spinal Cord","Stomach","None"],["FeCl","NaCl","KCl","None"]];
let counter = 0;
let i=0;
function next(){
if(i==3){
document.getElementById("question").innerHTML="YOUR SCORE IS : "+counter+"/6";
document.getElementById("a").parentNode.removeChild(a);
document.getElementById("b").parentNode.removeChild(b);
document.getElementById("c").parentNode.removeChild(c);
document.getElementById("d").parentNode.removeChild(d);
document.getElementById("a_text").parentNode.removeChild(a_text);
document.getElementById("b_text").parentNode.removeChild(b_text);
document.getElementById("c_text").parentNode.removeChild(c_text);
document.getElementById("d_text").parentNode.removeChild(d_text);
document.getElementById("submit").parentNode.removeChild(submit);
}else{
document.getElementById("question").innerHTML=ques[i];
document.getElementById("a_text").innerHTML = options[i][0];
document.getElementById("b_text").innerHTML = options[i][1];
document.getElementById("c_text").innerHTML = options[i][2];
document.getElementById("d_text").innerHTML = options[i][3];
i++;
}
}
function submitAns(){
selected();
next();
deselect();
}
let checkAnswers= document.querySelectorAll('.answer');
function deselect(){
checkAnswers.forEach(answerEl => answerEl.checked = false)
}
function selected(){
let marked
checkAnswers.forEach(answerEl => {
if(answerEl.checked) {
marked = answerEl.id
}
});
if(marked==ans[i-1]){
counter+=2;
}
}
next();

Explanation of the Code

1. There is an h2 tag for questions and 4 radio buttons for options.

2. We have used a submit button to submit the response and skip to the next question.

3. The CSS file is meant for styling the HTML, providing the container with appropriate margins, padding, font color, and font size.

Moving to the score calculation process. We store the answers provided by the test taker and then match them with the right options so that the final score can be calculated. To apply this, we have used

1. A method that is responsible for displaying questions and options stored in the array.

2. A method that verifies the chosen option with the right option and increments the score.

3. Finally, when the user reaches the end of the array of questions, it displays the final score to the user.

Output

Main Interface

Quiz Maker Using JavaScript

Quiz Maker Using JavaScript

Points To Remember

We have successfully built a Quiz maker Using JavaScript. This quiz maker displays multiple-choice questions and calculates the marks for the quiz, and displays it at the end of the quiz. It is a very easy way to conduct a quiz to check one’s knowledge. It also has a very user-friendly and simplistic interface.

So let us conclude by listing some key points to remember while building a Quiz Maker Using JavaScript.

  • Plan the structure of your quiz before you start writing the code. This includes the number of questions, the type of questions (e.g., multiple choice), and the layout of the quiz.
  • Start with the HTML structure for the quiz, including the questions, answers, and results. Make sure that each question and answer is properly formatted and labeled.
  • Use CSS to style the quiz and make it visually appealing. Consider the user experience and make sure that the quiz is easy to use and navigate.
  • Write the JavaScript code to handle quiz logic, including displaying questions, checking answers, and displaying results. Make use of variables, arrays, loops, and functions to make the code organized and easy to maintain.
  • Test the quiz regularly while building it to ensure that it is functioning correctly. This will help you catch any errors early on and make fixing them easier.
  • Add interactivity to the quiz to make it more engaging for the user. Consider using animations, sound effects, or feedback on incorrect answers to enhance the user experience.
  • Finally, don’t forget to make the quiz responsive so that it can be used on different devices and screen sizes.

By following these points, you can build a quiz maker using JavaScript that is both functional and engaging for users.

 

More JavaScript Projects>>>

You May Also Like To Create…