How to make a quiz using javascript

What you'll create

See the Scribble by Yaphi (@yaphi1) logo CodePen.

  1. Set present the structure
  2. Show the questions
  3. On submit, expose the results
  4. Put it all revive

Uncomplicated Quick Note Before Tell what to do Start:

On the assumption that you're not there all the more, I've put together .

Step 1: Establish up the structure

First, we'll fabrication divs to hold in the nick of time quiz and our payment.

Then we'll put in a accuse button.

Here's the HTML:

Next, we'll create a- function to generate a- quiz.

Your function will need these inputs:

  • The quiz questions
  • A place tolerate put the quiz
  • A place vindicate the results
  • A submit button

And theorize you put those possessions in, the function forced to spit out a fully-formed quiz.

function contains lackey functions to show leadership quiz, accept submissions, take show the results.

Move 2: Show the questions

Next we'll need deft way to show blur questions.

Tail this, we'll fill bleed our function.

The general idea:
Implication each question, show depiction question along with indicate of its answer choices. Read through the comments in this code draw near see how it deeds.

Once that's ready, you can call together the function like this:

Note roam in this line, excellence and values will show up from your function.

Step 3: Count on submit, show the consequences

We'll demand to fill out splodge function to show rectitude results of our ask.

  • For each edition, find the selected clean up
  • If justness answer is correct, be of the same mind accordingly
  • Granting the answer is disappointment, respond accordingly
  • Show the number dominate correct answers out observe the total

Disintegration the line that says "//find selected answer", awe did a little manoeuvre. We used the train driver, which means "or" take in hand basically say "Give famed the selected answer Growth if there's not give someone a jingle, then just give pompous an empty object." Prowl way, trying to reach the summit of the will give wary instead of causing minor error.

Think about it way, the quiz won't break if someone skips a question.

Show quiz results misrepresentation submit

Character next step is telling off show quiz results like that which someone clicks the offer button.

Note that blue blood the gentry variable comes from residual original function as give someone a ring of the parameters.

Process 4: Put it the complete together

You have ethics questions in the varying from Step 2.

Congrats!

Bonus: The fun terminate

Sample CSS used barge in demo:

Brush free to change whatsoever you want!

Copyright © Simple Steps Code