function calculate(options) { const num1 = options.num1; const operator = options.operator; const num2 = options.num2; let sum; // if the operator does not equal to plus minus multiply or subtract switch (operator) { case "+": sum = num1 + num2; break; case "-": sum += num1 - num2; break; case "*": sum += num1 * num2; break; case "/": sum += num1 / num2; break; default: sum = "Sorry no operator assigned"; break; } return sum; // dont forget to return sum after the switch has executed } console.log( calculate({ num1: 3, operator: "+", num2: 4, }) );
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter