Learn to Generate a Mathematical Quiz with Javascript

For a school project, I am tasked with developing a Math Quiz which showcases questions one at a time. The questions vary in type, including Multiple Choice, Narrative Response, Image Selection, Fill in the blank, and more.

I require assistance in creating a setup where each question is displayed individually, along with features like a scoreboard, real-time feedback on correct or incorrect responses, notification of pass or fail based on a score of 80% or higher, and an option for users to retake the quiz at the end.

This snippet represents my HTML file:

<!DOCTYPE html>
<html>
<head>

<title>Math Quiz!</title>
<link href ="quizCSS.css" rel ="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Lato" rel="stylesheet">
<script src = "MathQuiz.js" defer></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>


</head>

...
(coding example continues)

Here's a glimpse into my JavaScript code:

function check(){

    var question1 = document.quiz.question1.value;
    var question2 = document.quiz.question2.value;
    var question3 = document.quiz.question3.value;
    var question4 = document.quiz.question4.value;
    var question5 = document.quiz.question5.value;
    var correct = 0;

    if (question1 == "4") {
        correct++;
}
    ...
    (javascript function goes on)
    

Answer №1

If you're looking to hide and display elements using JavaScript and CSS, there are a few methods you can employ. One way is by changing the value of the element's display: block; property to hidden, and then switching it back to block when necessary. In this method, only one question will be visible at a time while the rest are hidden.

To achieve this functionality, you can use the following code snippets:

document.getElementsByID("myDiv1").style.display = "hidden";

and

document.getElementsByID("myDiv2").style.display = "block";

In the above code, myDiv1 represents the div containing questions that need to be hidden, while myDiv2 indicates the div containing the current question to display.

For a more in-depth guide on implementing this technique, refer to the w3schools Style display Property reference.

Answer №2

Javascript saves the day!

Just utilize $("#id").show() and hide():

var q = 1; //question #
function start() {
    $("#q2").hide();
    $("#q3").hide();
    //add more for more questions
}
function nextQ() {
    q++;
    update();
}
function update() {
    //hide all
    $("#q1").hide();
    $("#q2").hide();
    $("#q3").hide();
    //show next Q
    $("#q"+q).show();
}

Here is an HTML excerpt:

<button onclick="start();">Start</button>
<div id="q1">
    <!-- question -->
    1
    <button onclick="nextQ();">Next</button>
</div>
<div id="q2">
    <!-- question -->
    2
    <button onclick="nextQ();">Next</button>
</div>
<div id="q3">
    <!-- question -->
    3
    <button onclick="nextQ();">Next</button>
</div>

By the way, remember to use the onload attribute to execute start() immediately when the page loads.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is there a way to alter the format of a URL?

I'm utilizing the ytdl-core library to access a URL for audio content. The provided URL is: https://r2---sn-gwpa-w5py.googlevideo.com/videoplayback?expire=1612552132&ei=ZEMdYNnJDumPz7sPyrSLmAw&ip=49.36.246.217&id=o-AFQLS1cSUJ6_bXBjMOIiWk1N ...

Loading pages asynchronously using Ajax with added interactivity through custom JavaScript scripts

Using jQuery Masonry in conjunction with another JavaScript for voting, here is the code: <div id="contain"> <?php $result = $mysqli->query("SELECT * FROM posts ORDER BY id DESC LIMIT 0, 20"); while($row = mysqli_fetch_array($result ...

Unable to extract tabular information using BeautifulSoup on a specific webpage

I recently came across a helpful tutorial online about web scraping with Python using Beautiful Soup. The tutorial can be found at this link. Following the steps in the tutorial, I successfully scraped data from an HTML table. However, when I attempted to ...

View cards from a restricted Trello board without requiring visitors to have a Trello account or to authorize through a popup

I have a company with ongoing projects listed on a private Trello board. We are interested in showcasing these projects on our website in real-time by connecting to the board. After implementing this example, I can successfully retrieve and display the ca ...

Just dipping my toes into Django/Python and feeling completely bewildered by why my webpages won't connect

I'm having trouble with my webpage links. It seems like the file paths are incorrect. I've attempted to link three pages: The homepage The 'artists' icon on the top right of the homepage, linking to 'registerprofessional.html&apo ...

How can I implement a real-time progress bar using Rails and Jquery?

This is my first "general" Stack Overflow question - I'm not asking for someone to write code for me, just seeking advice I am searching for a way to incorporate a dynamic progress bar or thermometer (preferably using jQuery) that updates in real-tim ...

Creating an Ajax Post request for API invocation

I'm currently setting up an Ajax Post request to interact with an API. Here is a mock curl command for reference: curl -X POST \ --header 'Content-Type: application/json' \ --header 'Accept: application/json' \ --h ...

The jQuery element selection feature is not functioning

Within my HTML file, there lies a table with an empty tbody that is dynamically filled by jQuery once the document is fully loaded. The content of the table body is generated using a PHP script, where jQuery fetches the data via a simple GET request. Belo ...

Enable Parse5's case sensitivity

Recently, I've attempted to parse Angular Templates into AST using the powerful parse5 library. It seemed like the perfect solution, until I encountered an issue - while parsing HTML, it appears that the library transforms everything to lowercase. Fo ...

Failure of event watcher on dynamically updated content

Any help would be greatly appreciated! :) I am currently using JavaScript to dynamically add fields to a document, but I have noticed that the event listener only works on predefined fields. For instance, in the code snippet below, the 'lozfield&apo ...

Please be patient and allow the function to complete before moving forward

Currently, I am in the process of building my first nodejs/DynamoDB application and it involves working with JavaScript for the first time. My goal is to save a new record after scanning the table and using the results of the scan as a basis. After doing s ...

The npm audit tool uncovers unexpected dependencies in your project

When running npm audit, I receive warnings along with strange dependencies in the form of random hexadecimal strings. These strings change every time I run npm audit and are the same for all packages mentioned in the audit report. How can I remove these o ...

Could the datepicker/datetimepicker be delayed from initializing until an input is selected?

My screen contains numerous date/time pickers, but I'm experiencing performance issues on lower power machines. Is there a way to delay the loading of a date picker until a user interacts with a text input? The date/time picker I am currently using i ...

Is it possible to consistently show the placeholder in mat-select regardless of the item currently selected?

I am looking to keep the mat-select element displaying the placeholder at all times, even if an option has been selected. Below is my HTML code: <mat-select [formControlName]="'language'" placeholder="Language"> <mat-option value=" ...

Ways to retrieve text like innerText that functions across all web browsers

I need to retrieve the text from a Twitter Follow button, like on https://twitter.com/Google/followers Using document.getElementsByClassName("user-actions-follow-button js-follow-btn follow-button")[0].innerText correctly displays the text as: Follow ...

Combining model with a string in an expression in AngularJS version 1

Can this text be transformed into an expression using ecmaScript 2015? The || operator seems to be causing issues. {{ ::$ctrl.realEstateProjectCurrentProduct.housingTax + ' €' || $ctrl.noDataMessage }} ...

The div above the footer seems to be interfering with the styling of the footer. Is there a solution to

I am currently working on implementing a Help functionality for our users. In order to do this, I have placed a div right above my footer. However, I am facing an issue where the styling of my div is affecting the footer in ways that I cannot figure out. ...

Combine multiple key values from an array of objects into a single array

I have a set of key and value pairs that I need to filter based on whether the values are in an array or not, and then combine them into a single array. const holiday_expenses = { food: [{name: "abc", place: "xyz"}], travel: [{name: ...

Examining the process through which an element attains focus

Scenario I am working on a Backbone application that has an event listener set up for focus events on a textarea. Since Backbone relies on jQuery events, my main concern revolves around jQuery focus events. Inquiry Is there a method to determine how an e ...

Setting up a responsive footer using Bootstrap and responsive design involves following a few key steps

Seeking assistance with making the footer of my website responsive using a combination of Bootstrap and custom CSS. I am aiming for a result similar to the examples shown below, both optimized for Desktop and Mobile platforms. Your help in achieving this ...