With so much functionality you'll want to create a helper function. Something like:
JavaScript['math_arithmetic'] = function(block) {
// Basic arithmetic operators, and power.
const functionName = JavaScript.provideFunction_('mathArithmetic', `
function ${JavaScript.FUNCTION_NAME_PLACEHOLDER_}(a, b, op) {
if (typeof a !== 'number' || typeof b !== 'number') {
throw Error('Math operator requires numbers');
}
switch (op) {
case 'ADD':
return a + b;
case 'MINUS':
return a - b;
case 'MULTIPLY':
return a * b;
case 'DIVIDE':
if (!b) {
throw Error('Division by zero');
}
return a / b;
case 'POWER':
return Math.pow(a, b);
}
throw Error('Unknown math operation');
}
`);
const argument0 = JavaScript.valueToCode(block, 'A', order) || '0';
const argument1 = JavaScript.valueToCode(block, 'B', order) || '0';
const code = functionName + '(' + argument0 + ', ' + argument1 + ', "' + block.getFieldValue('OP') + '")';
return [code, JavaScript.ORDER_FUNCTION_CALL];
};