---
title: Make a Quiz
product: Form Library
description: Learn how to build interactive timed quizzes and tests for your JavaScript app with SurveyJS. Simply set the correctAnswer property while defining your surveys and calculate scores by comparing choices with the correctAnswer.
framework: React
source: https://surveyjs.io/form-library/examples/make-quiz-javascript/reactjs
index: https://surveyjs.io/form-library/examples/overview.md
---

# Make a Quiz (React)

SurveyJS allows you to create multi-step quizzes and tests using HTML and JavaScript. Whenever you need a qualitative pop-up quiz for students to test their understanding of the material, an industry-focused quiz to use in work meetings or presentations, or anything in between, set the [correctAnswer](https://surveyjs.io/Documentation/Library?id=Question#correctAnswer) property to turn your regular survey into a quiz. Compare choices with `correctAnswer` to calculate scores and use the [onComplete](https://surveyjs.io/Documentation/Library?id=surveymodel#onComplete) event to show scores immediately afterwards or save them to a database.

You can also customize your quiz to make it more entertaining and interactive by adding introduction and "Thank you" pages, a progress bar, and multiple quiz questions. If you want to pace your respondents and have them complete your quiz within a set time, you can build a quiz with a countdown timer for each individual page or the entire quiz.

For detailed instructions on how to make a quiz with JavaScript, refer to the following help topic: [Create a Quiz](https://surveyjs.io/Documentation/Library?id=design-survey-create-a-quiz).

## Files

### `public/index.html`

```html
<div id="surveyElement" style="position: absolute; top: 0; left: 0; right: 0; bottom: 0; min-height: 100%; height:100%"></div>
```

### `src/SurveyComponent.jsx`

```js
import React from "react";
import { Model } from "survey-core";
import { Survey } from "survey-react-ui";
import "survey-core/survey-core.min.css";
import "./index.css";
import { json } from "./json";

function SurveyComponent() {
    const survey = new Model(json);
    survey.onComplete.add((sender, options) => {
        console.log(JSON.stringify(sender.data, null, 3));
    });
    return (<Survey model={survey} />);
}

export default SurveyComponent;
```

### `src/index.css`

```css
/* You can add your custom CSS here. */
```

### `src/index.js`

```js
import React from "react";
import { createRoot } from "react-dom/client";
import SurveyComponent from "./SurveyComponent";

const root = createRoot(document.getElementById("surveyElement"));
root.render(<SurveyComponent />);
```

### `src/json.js`

```js
export const json = {
  "title": "American History",
  "showProgressBar": true,
  "progressBarLocation": "bottom",
  "showTimer": true,
  "timeLimitPerPage": 10,
  "timeLimit": 25,
  "firstPageIsStartPage": true,
  "startSurveyText": "Start Quiz",
  "pages": [
    {
      "elements": [
        {
          "type": "html",
          "html": "You are about to start a quiz on American history. <br>You will have 10 seconds for every question and 25 seconds to end the quiz.<br>Enter your name below and click <b>Start Quiz</b> to begin."
        },
        {
          "type": "text",
          "name": "username",
          "titleLocation": "hidden",
          "isRequired": true,
          "maxLength": 25
        }
      ]
    },
    {
      "elements": [
        {
          "type": "radiogroup",
          "name": "civilwar",
          "title": "When was the American Civil War?",
          "choices": [
            "1796-1803",
            "1810-1814",
            "1861-1865",
            "1939-1945"
          ],
          "correctAnswer": "1861-1865"
        }
      ]
    },
    {
      "elements": [
        {
          "type": "radiogroup",
          "name": "libertyordeath",
          "title": "Whose quote is this: \"Give me liberty, or give me death\"?",
          "choicesOrder": "random",
          "choices": [
            "John Hancock",
            "James Madison",
            "Patrick Henry",
            "Samuel Adams"
          ],
          "correctAnswer": "Patrick Henry"
        }
      ]
    },
    {
      "elements": [
        {
          "type": "radiogroup",
          "name": "magnacarta",
          "title": "What is Magna Carta?",
          "choicesOrder": "random",
          "choices": [
            "The foundation of the British parliamentary system",
            "The Great Seal of the monarchs of England",
            "The French Declaration of the Rights of Man",
            "The charter signed by the Pilgrims on the Mayflower"
          ],
          "correctAnswer": "The foundation of the British parliamentary system"
        }
      ]
    }
  ],
  "completedHtml": "<h4>You got <b>{correctAnswers}</b> out of <b>{questionCount}</b> correct answers.</h4>",
  "completedHtmlOnCondition": [
    {
      "expression": "{correctAnswers} == 0",
      "html": "<h4>Unfortunately, none of your answers is correct. Please try again.</h4>"
    },
    {
      "expression": "{correctAnswers} == {questionCount}",
      "html": "<h4>Congratulations! You answered all the questions correctly!</h4>"
    }
  ]
};
```

### `src/theme.js`

```js
export const themeJson = {};
```

### `package.json`

```json
{
  "dependencies": {
    "react": "latest",
    "react-dom": "latest",
    "survey-core": "latest",
    "survey-react-ui": "latest"
  },
  "devDependencies": {
    "react-scripts": "latest"
  }
}
```

## Other Frameworks

- [Angular](https://surveyjs.io/form-library/examples/make-quiz-javascript/angular.md)
- [Vue 3](https://surveyjs.io/form-library/examples/make-quiz-javascript/vue3js.md)
- [jQuery](https://surveyjs.io/form-library/examples/make-quiz-javascript/jquery.md)
- [Vanilla JS](https://surveyjs.io/form-library/examples/make-quiz-javascript/vanillajs.md)
