Arithmetic Calculator

Template
Function
114
Template
Function
by
Zoran Slamkov

Run simple arithmetic calculator functions: add, subtract, multiply and divide.

Created:

Heading

Voiceflow APIs used:

Channels
No items found.
Created By
Zoran Slamkov
This is some text inside of a div block.

Function Code Snippet


export default async function main(args) {
const { number1, number2, operation } = args.inputVars;

// Convert string inputs to numbers
const num1 = parseFloat(number1);
const num2 = parseFloat(number2);
let result;

switch (operation) {
case 'add':
result = num1 + num2;
break;
case 'subtract':
result = num1 - num2;
break;
case 'multiply':
result = num1 * num2;
break;
case 'divide':
result = num2 !== 0 ? num1 / num2 : 'Error: Division by zero';
break;
default:
result = 'Invalid operation';
}

return {
next: {
path: 'success'
},
outputVars: {
result: result.toString() // Convert the result back to a string if needed
},
trace: [{
type: 'text',
payload: {
message: `The result is: ${result}`
}
}]
};
}
copy-icon

Explore More Templates

Build and submit a Template to have it featured in the community.

ghraphic
No items found.
No items found.