javascript quiz program

JavaScript Quiz Program: We see many quizzes online and maybe play it every day. Because quiz is a way to test and improve your skills. Creating a JavaScript quiz is a cool learning exercise. It teaches you how to use events, manipulate the DOM, handle user input. In this article you will learn to create a javascript quiz app for your basic needs after that, you can use it on your website.

Today you will learn to create a similar quiz program with javascript. Previously I had already shared how to create a Quiz program in PHP, Now its time for JavaScript version. In this program, you can add a new question answer. If you have basic knowledge of JavaScript then you can understand the whole program easily. In this program, maybe 60-70% functions made in javascript & left other parts are in HTML & CSS. <h1> title and frame are built on HTML, style in CSS, left everything is built on JavaScript. Even, Question & Answers in JavaScript file.

Preview Of JavaScript Quiz App:

Here’s a look at how this program will look like after develop.

javascript quiz app

You May Also Like:

Registration Form with Validation Example and Source Code

Basic JavaScript Calculator Source Code | HTML, CSS

JavaScript Quiz Program Source Code

I had created 3 files for this program first one is for HTML file, the Second one is for CSS file, & the third one is for JavaScript ( JS ) file. I had put comments where you can add or change question answers in the JS file. You can change layout style in CSS file provided in this program.

Now create 3 files & follow the steps I have given below. File names are:

  • index.html
  • style.css
  • question.js

Index.html

Paste these codes are five below in index.html file you have created.

style.css

Now, pastes these codes in style.css file you have created before.

question.js

Finally, paste these following codes in question.js file.

That’s It. Now you’ve successfully created a quiz program in JavaScript. If you have any doubt comment down below. For more article/source code like this visit this blog regularly.

Thanks For Visiting, Keep Visiting.

28 COMMENTS

  1. Hey i wanted to ask if it is possible that you comment out the whole javascript part line by line because I have no idea what for what stands.

    • Quiz

      body {
      background-color: #413D3D;
      }

      .grid {
      width: 600px;
      height: 500px;
      margin: 0 auto;
      background-color: #fff;
      padding: 10px 50px 50px 50px;
      border: 2px solid #cbcbcb;

      }

      .grid h1 {
      font-family: “sans-serif”;
      background-color: #01BBFF;
      font-size: 60px;
      text-align: center;
      color: #ffffff;
      padding: 2px 0px;

      }

      #score {
      color: #01BBFF;
      text-align: center;
      font-size: 30px;
      }

      .grid #question {
      font-family: “monospace”;
      font-size: 30px;
      color: #01BBFF;
      }

      .buttons {
      margin-top: 30px;
      }

      #btn0,
      #btn1,
      #btn2,
      #btn3 {
      background-color: #01BBFF;
      width: 250px;
      font-size: 20px;
      color: #fff;
      border: 1px solid #1D3C6A;
      margin: 10px 40px 10px 0px;
      padding: 10px 10px;
      }

      #btn0:hover,
      #btn1:hover,
      #btn2:hover,
      #btn3:hover {
      cursor: pointer;
      background-color: #01BBFF;
      }

      #btn0:focus,
      #btn1:focus,
      #btn2:focus,
      #btn3:focus {
      outline: 0;
      }

      #progress {
      color: #2b2b2b;
      font-size: 18px;
      }

      Quiz Time !!!




      Question x of y

      function Quiz(questions) {
      this.score = 0;
      this.questions = questions;
      this.questionIndex = 0;
      }

      Quiz.prototype.getQuestionIndex = function () {
      return this.questions[this.questionIndex];
      }

      Quiz.prototype.guess = function (answer) {
      if (this.getQuestionIndex().isCorrectAnswer(answer)) {
      this.score++;
      }

      this.questionIndex++;
      }

      Quiz.prototype.isEnded = function () {
      return this.questionIndex === this.questions.length;
      }

      function Question(text, choices, answer) {
      this.text = text;
      this.choices = choices;
      this.answer = answer;
      }

      Question.prototype.isCorrectAnswer = function (choice) {
      return this.answer === choice;
      }

      function populate() {
      if (quiz.isEnded()) {
      showScores();
      }
      else {
      // show question
      var element = document.getElementById(“question”);
      element.innerHTML = quiz.getQuestionIndex().text;

      // show options
      var choices = quiz.getQuestionIndex().choices;
      for (var i = 0; i < choices.length; i++) {
      var element = document.getElementById("choice" + i);
      element.innerHTML = choices[i];
      guess("btn" + i, choices[i]);
      }

      showProgress();
      }
      };

      function guess(id, guess) {
      var button = document.getElementById(id);
      button.onclick = function () {
      quiz.guess(guess);
      populate();
      }
      };

      function showProgress() {
      var currentQuestionNumber = quiz.questionIndex + 1;
      var element = document.getElementById("progress");
      element.innerHTML = "Question " + currentQuestionNumber + " of " + quiz.questions.length;
      };

      function showScores() {
      var gameOverHTML = "Result”;
      gameOverHTML += ” Your scores: ” + quiz.score + “”;
      var element = document.getElementById(“quiz”);
      element.innerHTML = gameOverHTML;
      };

      // create questions here
      var questions = [
      new Question(“Hyper Text Markup Language Stand For?”, [“JavaScript”, “XHTML”, “CSS”, “HTML”], “HTML”),
      new Question(“Which language is used for styling web pages?”, [“HTML”, “JQuery”, “CSS”, “XML”], “CSS”),
      new Question(“Which is not a JavaScript Framework?”, [“Python Script”, “JQuery”, “Django”, “NodeJS”], “Django”),
      new Question(“Which is used for Connect To Database?”, [“PHP”, “HTML”, “JS”, “All”], “PHP”),
      new Question(“Webdevtrick.com is about..”, [“Web Design”, “Graphic Design”, “SEO & Development”, “All”], “All”)
      ];

      // create quiz
      var quiz = new Quiz(questions);

      // display quiz
      populate();

  2. Hello there, good day! I just want to ask, how about if the Questions and Answers are in database? how to get it from new Question( database questions and answers here )

  3. new Question(“Hyper Text Markup Language Stand For?”, [“JavaScript”, “XHTML”,”CSS”, “HTML”], “HTML”),
    new Question(“Which language is used for styling web pages?”, [“HTML”, “JQuery”, “CSS”, “XML”], “CSS”),
    new Question(“Which is not a JavaScript Framework?”, [“Python Script”, “JQuery”,”Django”, “NodeJS”], “Django”),
    new Question(“Which is used for Connect To Database?”, [“PHP”, “HTML”, “JS”, “All”], “PHP”),
    new Question(“Webdevtrick.com is about..”, [“Web Design”, “Graphic Design”, “SEO & Development”, “All”], “All”)

    isake niche extra question add nahi ho raha?
    kaise add karana he plz help us

  4. Hello

    Thank you for sharing this with us, I am new in JS and i have a question, how can you think like that. i tried for week to create this game but i failed?
    and please if you can add some commentaires inside the code to be more understood.
    Thanks

  5. i have a problem with the marks?how many marks per one question?if i change this to 10 question there is a error in marks?

  6. Hi bro, shout out from portugal, i have a question for you.

    Im doing a project based on your quiz and when i change the design of it to a more apelative aspect my js doesnt work, i mean i cant answer the questions and i need some help.
    May i send you my project via email and u help me with that?

    Thx.

  7. Hi ,
    thank you so much for your quiz! It was just the thing I was looking for, especially with the layout and how it works!
    The thing is that I’m trying to:
    1) first the user has an option of 2 buttons to click on and then a table will appear in that same place (there should be two tables, depending on what the user clicks; one table will show up) and only once an img (of a person) from that table is selected by the user, the quiz will start, but based only n the person selected. If the user were to choose a different image, then a different quiz would start.
    2) I would like to do that there is one answer that comes up as wrong and one answer right, and the rest neutral, and for the user immediately to see the type of answer they selected (ie if correct, wrong or nothing).
    how would I write out the code for these please?

  8. When I open my index.html file, I get a window with the centered title box, 4 blank clickable boxes that don’t do anything, and it says “question x of y” below them. How do I get the quiz to start and function properly?

    • You need to link the .js and .css file correctly! Make sure you’re spelling it correctly, I had the same problem and this is what solved it for me. Hope that helps!

  9. @fuckyouindianguy You clearly know nothing of the coding world, coders help each other out, and that is how people learn, When someone creates something new or finds something that is either problematic or difficult to do they put it online so others can learn from it and as for calling us “nerds” without us “nerds” well, you were here too buddy…

  10. Very good one and straight forward to understand. Can someone assist me on the below modification to the code
    – user session captured
    – saving the result into the database against the user.

LEAVE A REPLY

Please enter your comment!
Please enter your name here