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

Using websockets in a React client application

Attempting to establish a connection with a backend layer running on localhost, here is the provided source code: const { createServer } = require("http"); const cors = require("cors"); const photos = require("./photos"); const app = require("express")( ...

Unidentified event listener

I am currently facing an issue where I keep getting the error message "addEventListerner of null" even though I have added a window.onload function. The page loads fine initially but then the error reoccurs, indicating that the eventListener is null. Str ...

Managing session timeouts during an AJAX call triggered by the Kendo ComboBox read action

Looking for a solution to handle session timeout specifically with kendo combo-box implementation? Here is the HTML code snippet for my kendo combobox: @(Html.Kendo().ComboBoxFor(model => model.PropertyName) .AutoBind(tru ...

Concealed upper TD boundary in IE 8

I've been struggling all day to figure out this style issue without any success. http://jsfiddle.net/9HP9Q/1/ The table lines should have gray borders around them. It's working fine on all browsers except Internet Explorer 8. .order_table tab ...

Tips for deleting directory path from a file name

Similar Question: How to extract file name from full path using PHP? echo '<td width="11%" class="imagetd">'. ( ( empty ($arrImageFile[$key]) ) ? "&nbsp;" : htmlspecialchars( is_array( $arrImageFile[$key] ) ? implode(",", $arrImag ...

Building an HTML table dynamically with JavaScript

Can anyone help me figure out why my JavaScript code isn't populating the HTML body table as expected? var shepard = { name: "Commander", victories: 3, ties: 1, defeats: 6, points: 0 }; var lara = { name: "RaiderOfTombs", victories: ...

Adjust the padding of buttons by updating the SASS variables

Struggling to locate the predefined SASS variable that controls the padding on the left and right of btn-lg. The only thing I could come across is: .btn-lg { @include button-size($btn-padding-y-lg, $btn-padding-x-lg, $btn-font-size-lg, $btn-line-height- ...

Encountering an issue with WebDriver in the realm of JavaScript

I am struggling to use JavaScript to locate specific controls and send values to them. One example is changing the text in a textbox with the ID "ID" to "123456". Below is the code I tried: ((IJavaScriptExecutor)driver).ExecuteScript("document.getElement ...

Avoid triggering the parent modal to close when a child element is clicked in VueJS

In my Vue application, there is a situation where I have a button called "detach button" with a @click function inside an element that is clickable. When clicking on this parent element, it triggers another @click function and toggles a Bootstrap modal eve ...

Manipulate the value of an HTML element's style parameter with jQuery

How do I change an element's style property using jQuery? This is the HTML code I am working with: <div id="template" style="width:200px; height:200px; border:none;">blabla...</div> I attempted to do this: $('#template').attr ...

Can a search engine algorithm be developed to display an iframe based on the keywords entered in the search query?

Is it possible to create a search engine algorithm in HTML, CSS, and JavaScript that takes keywords from a search bar input and displays the best solution (or solutions) through iframes? html, css, javascript Below is the code for the iframes and search ...

Creating a spacious text box for an enhanced Ajax search feature

I'm currently working on an AJAX application that allows users to input the name of a movie, and then loads results from the database through jquery using a PHP API. However, I'm facing a challenge in implementing a text box with the following re ...

Enhancing jQuery OrgChart with personalized AJAX request headers

Is there a way to set request headers on an ajax call using the OrgChart library? I attempted the following: $('#chart-container').orgchart({ 'data' : '/api/v1/profiles/orgchart/', 'beforeSend&ap ...

What is the best way to anchor the components to a specific location on the screen in ASP.NET?

Currently I am working on creating a registration page in asp.net. I have been using panels to group the components such as labels, dropdown lists, and text boxes. However, when I run the page, I noticed that the positions of these components keep changing ...

Obtain the results of your query neatly organized in a visually appealing table

I need help with displaying query results in a table. The HTML form includes a dropdown menu for selecting an institution name, and the table should show the persons associated with that institution. After clicking the submit button, the query result from ...

<head> Javascript codes are failing to run

Currently utilizing JQuery Mobile, my goal is to navigate between pages with minimal script loading each time a page loads. Essentially, I want to import all the general scripts (such as JQuery.js, jquery_mobile.js, main.js, etc.) ONCE for all pages. In m ...

Issues with Curved Corner Touchdown Feature

Having trouble with getting the round css to work properly in the below code It seems to be only applying the background color, but not the border styling The border code is on the same line as the round css: style=" border : 1px solid #D6D6D6; and t ...

Using the Play framework to showcase data in an HTML table

I have a project where I am working with multiple tables, one of which is a Person table containing id and name attributes. I have successfully retrieved the data from the database using MySql and converted it into JSON. However, I am struggling to find a ...

Saving maps on React Native can be done easily using AsyncStorage

I am facing an issue with saving user inputs as a JS map using AsyncStorage in my React Native app. Despite no errors during the saving process, I encountered "[object Map]" when attempting to retrieve the data. Here is a simplified version of my user map ...

Building an "update" form using a lightbox/modal window in a node.js environment

I'm currently diving into Node.js and working on a project that involves using node along with the powerful Jade templating engine. My focus now is to deliver an interactive user experience by implementing a lightbox feature that enables users to edit ...