How to Create an Online Quiz or Assessment test using SurveyJS and add Scoring and Timing to It (Part 1)
Tags: javascript, form, quiz, onlinetest
Published: 2023-03-02 10:05:47

![Create an online assessment test with a limited timing - SurveyJS](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k9o3lnmok0oxonthuclk.png)

## Introduction to the SurveyJS libraries and their benefits in education.
Online quizzes are a great way to engage your students and test their knowledge on a particular topic. With [SurveyJS](https://surveyjs.io/) you can create multiple online assessment forms, add timing and scoring to them, customize their look, and provide immediate and personalized feedback to each student once they completed a test. 

In the first part of the article, we will explore the SurveyJS product family and guide you through the process of creating a timed online quiz using SurveyJS Form Library. The second part will be devoted to various scoring features that the Survey Creator library has to offer, including how to assign a score to a certain question or to each or some of its choice options, calculate the total score, and more.

## Why choose SurveyJS for online assessment forms?

SurveyJS is a product family of four open-source JavaScript libraries that enable you to build a full-cycle form management system within your in-house infrastructure:

* [**SurveyJS Form Library**](https://surveyjs.io/form-library) - Enables you to configure and run forms and surveys on your website. (Free, open-source, under the MIT license).
* [**SurveyJS Creator**](https://surveyjs.io/survey-creator) - An embeddable no-code form/survey builder with WYSIWYG UI. (Free-to-use here, but requires a paid developer license for commercial use).
* [**SurveyJS Dashboard**](https://surveyjs.io/dashboard) - Visualizes survey results as charts/tables in a dashboard.
* [**SurveyJS PDF Generator**](https://surveyjs.io/pdf-generator) - Exports survey responses as PDF files.

Being a client-side component, [SurveyJS Form Library](https://surveyjs.io/form-library/documentation/overview) supports native integration with [React](https://surveyjs.io/form-library/documentation/get-started-react), [Angular](https://surveyjs.io/form-library/documentation/get-started-angular), [Vue.js](https://surveyjs.io/form-library/documentation/get-started-vue), [Knockout](https://surveyjs.io/form-library/documentation/get-started-knockout), and for those, who prefer to use [jQuery](https://surveyjs.io/form-library/documentation/get-started-jquery) or any other JavaScript framework, it offers a special package that utilizes Knockout to render form widgets.
Once you've created a test app for your preferred frontend technology, you are ready to get started with SurveyJS.

### Install and configure SurveyJS Form Library

As an example, we are going to add the library to a sample React app. But you can find a [dedicated step-by-step guide on how to get started with SurveyJS](https://surveyjs.io/form-library/documentation/get-started) for other JavaScript frameworks in the Documentation section.

1.1 Install the [survey-react-ui](https://www.npmjs.com/package/survey-react-ui) npm package using the following command:
```
npm install survey-react-ui - save
```

1.2 Configure the style of your form by referencing either of the two built-in themes (CSS files):

```
// Default V2 theme
import 'survey-core/defaultV2.min.css';
// Modern theme
// import 'survey-core/modern.min.css';
```

### Create a form model in JSON
1.3 Now, we are ready to create a model of our test, which will describe its layout and contents. The simplest way to create a [form model](https://surveyjs.io/form-library/documentation/api-reference/survey-data-model) is to use a JSON-based schema. This schema contains different sections and parts of our form. Here is what such a schema looks like for a sample 2-page geography test:
```
{
  title: "Geography test",
  description:
    "Test how well you know geography with this multiple-choice challenge and learn about the continents, countries, oceans and vast bodies of water that are part of planet Earth.",
  logo:
    "https://api.surveyjs.io/private/Surveys/files?name=7c80d870-2ce5-450a-8013-b40725991e89",
  logoPosition: "right",
  firstPageIsStarted: true,
  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 are correct. Please try again.</h4>"
    }
  ],
  pages: [
    {
      name: "startPage"
    },
    {
      name: "page1",
      elements: [
        {
          type: "radiogroup",
          name: "count_continents",
          title: "How many continents are there?",
          correctAnswer: "7",
          choices: ["4", "6", "5", "7"],
          colCount: 2
        },
        {
          type: "dropdown",
          name: "largest_continent",
          title: "What is the largest continent by land area?",
          correctAnswer: "Asia",
          choices: ["Antarctica", "Europe", "Asia", "Africa"]
        }
      ]
    },
    {
      name: "page2",
      elements: [
        {
          type: "radiogroup",
          name: "count_oceans",
          title: "How many oceans are there?",
          correctAnswer: "5",
          choices: ["3", "5", "4", "6"],
          colCount: 2
        }
      ]
    }
  ],
  maxTimeToFinish: 90,
  maxTimeToFinishPage: 30,
  showTimerPanel: "top"
}
```

Here’s a brief explanation of every property of the model:

* [**title**](https://surveyjs.io/form-library/documentation/api-reference/survey-data-model#title): Displays the form’s title.
* [**description**](https://surveyjs.io/form-library/documentation/api-reference/survey-data-model#description): Explanatory text displayed under the title.
* [**logo**](https://surveyjs.io/form-library/documentation/api-reference/survey-data-model#logo): Gets or sets a survey logo.
* [**logoPosition**](https://surveyjs.io/form-library/documentation/api-reference/survey-data-model#logoPosition): Gets or sets a survey logo position.
* [**pages**](https://surveyjs.io/form-library/documentation/api-reference/survey-data-model#pages): Describes all the pages of the form. Here we have two different pages.

As you can see, there are two question types: a [radio button group](https://surveyjs.io/form-library/documentation/api-reference/radio-button-question-model) and a [single selection drop-down menu](https://surveyjs.io/form-library/documentation/api-reference/dropdown-menu-model). However, all other available [question types](https://surveyjs.io/form-library/documentation/api-reference/question#getType) can also be used to create a test. Please refer to the [Page model](https://surveyjs.io/form-library/documentation/api-reference/page-model) and [Question model](https://surveyjs.io/form-library/documentation/api-reference/question) API references to learn more about methods, properties, and events used in those classes. To turn a form into a test, the [correctAnswer](https://surveyjs.io/form-library/documentation/api-reference/question#correctAnswer) property has been specified for each question of the form.

### Add time limits for self-pacing

Our sample test is also timed: the [maxTimeToFinish](https://surveyjs.io/form-library/documentation/api-reference/survey-data-model#maxTimeToFinish) property limits the time a student is allowed to spend to finish the entire test, and the [maxTimeToFinishPage](https://surveyjs.io/form-library/documentation/api-reference/survey-data-model#maxTimeToFinishPage) property limits the time allowed to answer all questions of a page. SurveyJS Form Library ships with other timing features, including test pausing and resuming that you can learn about in the [Specify Time Limits](https://surveyjs.io/form-library/documentation/design-survey/create-a-quiz#specify-time-limits) guide.

### Create personalized feedback pages

To make sure each test taker receives personalized feedback upon test completion, we used the [completedHtmlOnCondition](https://surveyjs.io/form-library/documentation/api-reference/survey-data-model#completedHtmlOnCondition) property. Let’s take a closer look at how it works and what other options to [configure the feedback page](https://surveyjs.io/form-library/documentation/design-survey/create-a-multi-page-survey#complete-page) are. By default, a SurveyJS form displays a basic ‘Thank you’ message at the end. If you want to customize the contents of this message, simply populate the [completedHtml property](https://surveyjs.io/form-library/documentation/api-reference/survey-data-model#completedHtml) with custom HTML markup. And if you deal with a test and you want to make sure each of your respondents sees an exact number of correct or incorrect answers, the markup should include one or some of the following placeholders:

* `{correctAnswers}` — The number of correct answers.
* `{incorrectAnswers}` — The number of incorrect answers.
* `{questionCount}` — The number of questions.

```
const surveyJson = {
    completedHtml: "<h4>You got <b>{correctAnswers}</b> out of <b>{questionCount}</b> correct answers.</h4>",
...
};
```
If you want to go further and provide custom feedback _based_ on the number of correct/incorrect answers, populate the [completedHtmlOnCondition](https://surveyjs.io/form-library/documentation/api-reference/survey-data-model#completedHtmlOnCondition) array with a series of objects consisting of a Boolean expression (make sure to use the placeholders mentioned earlier) and HTML markup. Once an expression evaluates to true, a test taker sees the text from the related object in the ‘Thank you’ message.

```
const surveyJson = {
    completedHtmlOnCondition: [{
        expression: "{correctAnswers} == 0",
        html: "<h4>Unfortunately, none of your answers are correct. Please try again.</h4>"
    }, {
        expression: "{correctAnswers} == {questionCount}",
        html: "<h4>Congratulations! You answered all the questions correctly!</h4>"
    }]
};
```

### Create a Survey Model and Render a Survey

1.4 Now, when the data schema is configured, you want to instantiate a model by passing the model schema to the Model constructor:

```
import 'survey-core/defaultV2.min.css';
import { Model } from 'survey-core';

const surveyJson = {
  //...
}

function App() {
   const survey = new Model(surveyJson);
   return …;
}
export default App;
```

1.5 To render a survey, import the Survey component, add it to the template, and pass the model instance you created in the previous step to the component’s model attribute:

```
import 'survey-core/defaultV2.min.css';
import { Model } from 'survey-core';
import { Survey } from 'survey-react-ui';


const surveyJson = {
   //...
};


function App() {
  const survey = new Model(surveyJson);

  return <Survey model={survey} />;
}


export default App;
```

You will also need to configure the storage of your test result. Please follow the [Handle Survey Completion](https://surveyjs.io/form-library/documentation/get-started-react#handle-survey-completion) guide to get the instructions.

### A Basic Geography Test Demo
Run the demo below to take the test and challenge your knowledge!

{% embed https://codesandbox.io/embed/reverent-wilbur-shpc5x?fontsize=14&hidenavigation=1&theme=dark %}

That’s all for now.

In the second part of the article, we will show you how to add scoring to an assessment form.