Create derived answers based on other answers within the flow

**Calculated answers** allow you to define new answers based on other answers present within the flow, by writing simple javascript functions that are evaluated within Formsort.

Common uses of calculated answers include:

Formatting answers

Capturing condition logic that cannot be easily expressed

Performing math on numbers or dates

Calculated answers behave just like any other answer: they can be used for conditions, templated into most text, and are sent to your analytics and integrations.

From the **Schema **tab, select **Calculated variables**, and click **Add calculated variable...**

Sets the expected data type that we are expecting the calculation to return: `string`

, `number`

, or `boolean`

If **Is array? **is enabled, then we will expect the response to contain multiple answers.

The **Getter function body **contains the Typescript code function body for the calculated answer.

For example, the following getter function body will result in the date 6 months from the current date:

// Getter function bodyfunction myFunction(): string {const today = new Date();const d = today.getDate();const future = new Date(today.setMonth(today.getMonth() + 6));if (future.getDate() != d) {future.setDate(0);}return future.toLocaleDateString();}

To use existing answers within a getter function body, add the answers as variables. The following calculated answer will calculate the length of the answer variable labelled `first_name`

For another example, we calculate an age from a date stored in an answer variable labelled `dob`

, referring to it within the function body itself :

// Getter function bodyfunction myFunction(dob: string): number {const ageDifMs = Date.now() - (new Date(dob)).getTime();const ageDate = new Date(ageDifMs);return Math.abs(ageDate.getUTCFullYear() - 1970);}

The `myFunction() { ... }`

part of the getter function body is auto-generated and cannot be modified.

If a calculated answer has no answer dependencies, it is evaluated at the time that the flow loads.

For calculated answers with dependencies, by default, calculated answers are only evaluated once all of their required (non-*optional*) input answers are defined. If any of the required input answers become undefined, the calculated answer is cleared.

Imagine the following calculated answer, which uses the javascript Math.max function to find the larger of two numbers, and we'll call `largest_number`

:

// Getter function bodyreturn Math.max({{number_a}}, {{number_b}});

If only one or none of the input answers are defined, `largest_number`

will remain undefined.

number_a: undefinednumber_b: undefinedâ€‹largest_number: undefinedâ€‹---â€‹number_a: 17number_b: undefinedâ€‹largest_number: undefined

Only when all of the input answers are defined will the calculation occur.

number_a: 17number_b: 48â€‹largest_number: 48

For more control over when the calculated answer is evaluated, you can set a condition using **Is conditional?**

When using conditional evaluation, you must take care to handle `undefined`

answers within the getter function body.

Using the above example of `largest_number`

, we could add a condition using Advanced logic to run the calculation whenever either number is defined.

// Condition for either number_a OR number_b being defined{"$or": ["number_a": { "$exists": true },"number_b": { "$exists": true },}}

Now that we're evaluating the calculation even when the inputs are undefined, we need to change the getter function body to handle this case.

// Getter function bodyif ({{number_a}} === undefined) {return {{number_b}};} else if ({{number_a}} === undefined) {return {{number_a}};}return Math.max({{number_a}}, {{number_b}});

If a calculated answer was calculated in a previous session, and the responder returns to the flow, the default behavior is *not *to re-calculate the answer.

To always recalculate the answer on load, enable **Re-calculate on load**. This is useful for situations when calculated answers are not idempotent, meaning that repeated invocations do not result in the same result, such as answers depending on the current date or time.