---
title: Quiz with Instant Results
product: Form Library
description: Allow students to get immediate feedback on each answer they give. Create a custom quiz or test and add the correctAnswer property to each question to ensure instant answer validation. View free demo for JavaScript to learn more.
framework: React
source: https://surveyjs.io/form-library/examples/create-quiz-with-immediate-results/reactjs
index: https://surveyjs.io/form-library/examples/overview.md
---

# Quiz with Instant Results (React)

Online quizzes are a great way to engage your students and test their knowledge on a particular topic. With SurveyJS, you can create multiple [online assessment forms](https://surveyjs.io/form-library/examples/make-quiz-javascript/reactjs), add timing and [scoring](https://surveyjs.io/form-library/examples/create-a-scored-survey/reactjs), customize their appearance, and give personalized feedback to each student once they have completed a test or right after they answered a question. This demo shows how to create an assessment test that displays instant results. Learners can see whether their answer is correct or incorrect immediately after they give it.

## Create a Regular Quiz

For your assessment test, you can leverage all available questions types: single- or multiple-choice questions, true/false questions, fill-in-the-blanks questions, and many more. Specify the [`correctAnswer`](https://surveyjs.io/form-library/documentation/api-reference/question#correctAnswer) property for each question. For detailed instructions on how to make a quiz in JavaScript, refer to the following help topic: [Create a Quiz](https://surveyjs.io/form-library/documentation/design-survey/create-a-quiz).

## Display Results Immediately

To display the response result, check whether a question was answered correctly by calling a question's [`isAnswerCorrect()`](https://surveyjs.io/form-library/documentation/api-reference/question#isAnswerCorrect) method and show the check result. Handle the survey's [`onValueChanged`](https://surveyjs.io/form-library/documentation/api-reference/survey-data-model#onValueChanged) event to perform these actions immediately after a question value is set. In this demo, "Correct" and "Incorrect" labels indicate correct and incorrect answers. These labels are added to [question titles](https://surveyjs.io/form-library/documentation/api-reference/question#title) (see the `changeTitle` function).

You should also disallow users to change their answer. Configure a question's [`enableIf`](https://surveyjs.io/form-library/documentation/api-reference/question#enableIf) expression to disable the question after it is answered.

Optionally, you can handle the survey's [`onTextMarkdown`](https://surveyjs.io/form-library/documentation/api-reference/survey-data-model#onTextMarkdown) event to format the correct/incorrect label text with HTML and CSS.

## 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));
    });
    const correctStr = "Correct";
    const incorrectStr = "Incorrect";
    
    // Builds an HTML string to display in a question title
    function getTextHtml (text, str, isCorrect) {
        if (text.indexOf(str) < 0)
            return undefined;
    
        return text.substring(0, text.indexOf(str)) +
            "<span class='" +  (isCorrect ? "correctAnswer" : "incorrectAnswer" ) + "'>" +
                str +
            "</span>";
    }
    
    // Adds "Correct" or "Incorrect" to a question title
    function changeTitle (q) {
        if (!q) return;
    
        const isCorrect = q.isAnswerCorrect();
        if (!q.prevTitle) {
            q.prevTitle = q.title;
        }
        if (isCorrect === undefined) {
            q.title = q.prevTitle;
        }
        q.title =  q.prevTitle + ' ' + (isCorrect ? correctStr : incorrectStr);
    }
    
    survey.onValueChanged.add((_, options) => {
        // Change the quesion title when the question value is changed
        changeTitle(options.question);
    });
    
    survey.onTextMarkdown.add((_, options) => {
        const text = options.text;
        let html = getTextHtml(text, correctStr, true);
        if (!html) {
            html = getTextHtml(text, incorrectStr, false);
        }
        if (!!html) {
            // Set an HTML string with the "Correct" or "Incorrect" suffix for display
            options.html = html;
        }
    });
    return (<Survey model={survey} />);
}

export default SurveyComponent;
```

### `src/index.css`

```css
.correctAnswer {
    color: #00613D;
    text-transform: uppercase;
}
.incorrectAnswer {
    color: #9C0227;
    text-transform: uppercase;
}
.sd-element__title.sd-element__title--disabled {
    opacity: 1;
}
```

### `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",
          "enableIf": "{civilwar} empty"
        }
      ]
    },
    {
      "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",
          "enableIf": "{libertyordeath} empty"
        }
      ]
    },
    {
      "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",
          "enableIf": "{magnacarta} empty"
        }
      ]
    }
  ],
  "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/create-quiz-with-immediate-results/angular.md)
- [Vue 3](https://surveyjs.io/form-library/examples/create-quiz-with-immediate-results/vue3js.md)
- [jQuery](https://surveyjs.io/form-library/examples/create-quiz-with-immediate-results/jquery.md)
- [Vanilla JS](https://surveyjs.io/form-library/examples/create-quiz-with-immediate-results/vanillajs.md)
