Auto-evaluation by Javascript function

Develop your own evaluator javascript function

Created by Mustafa Ekim / March, 2023

It is possible to create a personalized JavaScript function that can automatically assess the provided response for an input question associated with Text-field, Text area, or Numeric input types and generate a percentage score as a result.

Creating a custom Javascript function

Your organization can develop and save customized JavaScript functions internally. To access these functions, please follow the steps outlined below:

  1. Select "Organization" from the primary navigation menu located on the left-hand side of the Testinvite application.
    1. Select Snippets from the presented options.
      1. Select Functions from the presented snippet options.

        A catalog of previously established custom functions will appear. To generate a new function, click on the red plus icon located in the lower-right corner of the page.  A new form will appear, accompanied by a code editor.

        1. Provide a title for your custom function.
          1. Provide a description for your custom function.
            1. Compose a personalized code for the function.

              Composing the function code

              The function must be designated as anonymous, and it should permit only one parameter, which can be named "data." The question's given answer will be transferred to your function via the "data" parameter, allowing you to analyze its contents, create a customized algorithm, and return an object with a single property called "score." The value of the "score" property should be a numeric value between -1 and 1.

              Example function code

              function anonymous(data) {
                  if (!data.response) return { score: 0 };
                  try {
                      let answer = parseInt(data.response)
                      if (answer > 1000) return { score: 1 }
                      else if (answer > 500) return { score: 0.5 }
                      else return { score: 0 }
                  }
                  catch (err) {
                      return { score: -1 }
                  }
              }

              The "data" parameter is anticipated to have a "response" property inside it, which may either contain an "undefined" value or a string value.

              The function is executed within a secure container and cannot access any environmental variables or resources. All of the logic for assessing the provided response must be enclosed solely within the function's code, and no external resources or APIs can be accessed.

              Emulating the execution of the function proving the data parameter

              By selecting the "Emulator" button situated at the lower-right corner of the code editor, you can experiment with your function by crafting a data parameter in JSON format.

              1. Create a JSON object with one property called "response" and allocate it a string value.
                1. Click on the Apply button.
                  1. Observe the output area. The score property must be a number between -1 and 1.

                    Linking your personalized function with an input question as the automated evaluator.

                    If the input type is Text-field, Text area, or Numeric, your function can be connected with the input question as an automated evaluator.

                    If you revise your function code following its connection to your question, select the refresh button to apply the changes to your question.

                    Previewing your question and the operation of auto-scoring

                    Once you have linked your customized function with the input question, you may preview the question, provide a sample response, and select the Previewing score button located at the lower-right corner to test how your response is being automatically evaluated by your personalized JavaScript function.

                    Go Back
                    Talk to a representative
                    Figure out if TestInvite is a good match for your organization