The Arithmetic/arithmeticOperations extension performs a basic arithmetic operation on two provided values and returns either the calculated result or an error message.
This is useful when a journey needs to calculate, compare, or validate numeric values such as totals, balances, quantities, or percentages using addition, subtraction, multiplication, or division.
Parameters:
-
operandrequired, a string that specifies the operation to perform. Supported values areadd,subtract,multiply, anddivide; -
Number1required, the first numeric value. The source code parses this input withparseFloatbefore calculation; -
Number2required, the second numeric value. The source code parses this input withparseFloatbefore calculation.
Note: The operation name must match one of the supported lowercase values exactly. Values such as Add, plus, or division are not accepted by the source code.
How to apply this to your journey
Use the extension in a journey by calling Arithmetic/arithmeticOperations with the execute command. Pass each value to the matching extension input using as inputName.
Note: This extension returns the result directly for valid calculations. For invalid numbers, unsupported operations, or division by zero, the source catches the error and returns the error message as the output value.
execute "Arithmetic/arithmeticOperations" using "add" as operand, "5" as Number1, "10" as Number2 returning $resultexecute "Arithmetic/arithmeticOperations" using "divide" as operand, "10" as Number1, "5" as Number2 returning $resultYou can also pass stored journey variables into the extension when the operation or numbers come from previous journey steps.
execute "Arithmetic/arithmeticOperations" using "$operation" as operand, "$firstValue" as Number1, "$secondValue" as Number2 returning $resultstore value "multiply" in $operation
store value "10" in $firstValue
store value "5" in $secondValue
execute "Arithmetic/arithmeticOperations" using "$operation" as operand, "$firstValue" as Number1, "$secondValue" as Number2 returning $resultExample output for multiplying 10 by 5:
50This extension does not require any external resource.
The extension should be configured as:
- Run asynchronously: No
- Scope: Global
Limitation: This extension performs simple JavaScript numeric calculations only. It uses parseFloat, so values with unsupported formatting, comma separators, currency symbols, localized decimal separators, or trailing non-numeric text may be rejected or parsed differently than expected. The operation input is case-sensitive and only supports add, subtract, multiply, and divide. Division by zero returns an error message. Because the calculation uses standard JavaScript numbers, very large values, high-precision decimal values, and financial calculations can be affected by floating-point precision behavior. Cross-browser note: This extension does not intentionally use browser-specific APIs beyond standard JavaScript. Still, validate it in the browser/device configurations used by your plans when it is combined with page state, network calls, external libraries, or DOM-dependent journey steps.
Add the extension to your Virtuoso instance
Select the domain that matches your Virtuoso account.
View source
Last updated: 04/06/2026
Resources:
This extension does not require any external resource.
// Function to perform arithmetic operations
function calculate(operation, num1, num2) {
// Convert inputs to numbers using parseFloat
num1 = parseFloat(num1);
num2 = parseFloat(num2);
// Check for invalid number inputs
if (isNaN(num1) || isNaN(num2)) {
throw new Error("Invalid numbers provided.");
}
switch (operation) {
case 'add':
return num1 + num2;
case 'subtract':
return num1 - num2;
case 'multiply':
return num1 * num2;
case 'divide':
if (num2 === 0) {
throw new Error("Division by zero is not allowed.");
}
return num1 / num2;
default:
throw new Error("Invalid operation. Please use 'add', 'subtract', 'multiply', or 'divide'.");
}
}
// Example usage
const operation = operand; //"add", "subtract","multiply","divide"
const num1 = Number1; // Example input as string
const num2 = Number2; // Example input as string
try {
const result = calculate(operation, num1, num2);
return(result);
} catch (error) {
return(error.message);
}
Comments
0 comments
Please sign in to leave a comment.