Repository navigation
#210 Calculate Using String Operation
Adam Kubiś edited this page Oct 28, 2026
·
1 revision
Create a function that takes two numbers and a mathematical operator and returns the result.
Current source and tests | Catalog | Testing and tooling
From the cloned repository root, after installing dependencies:
node "Edabit/210 Calculate Using String Operation.js"Assertions are kept alongside the implementation and throw on failure. A successful run exits with code 0. Run every exercise with npm test or both lint and tests with npm run check.
This is the complete executable file, including its imports and inline test cases. Run it from the repository, not as an isolated file copied out of the wiki. Line endings and trailing whitespace are normalized; executable tokens are unchanged.
//Create a function that takes two numbers and a mathematical operator and returns the result.
function calculate(num1, num2, op) {
switch (op) {
case "+": return num1 + num2;
case "-": return num1 - num2;
case "*": return num1 * num2;
case "/": return num1 / num2;
case "%": return num1 % num2;
default: return null;
}
}
import { assertEquals } from '../testHelper.js';
assertEquals(calculate(24, 100, "-"), -76)
assertEquals(calculate(-20, -30, "+"), -50)
assertEquals(calculate(1500, 5, "/"), 300)
assertEquals(calculate(38, 3, "*"), 114)
assertEquals(calculate(49, 5, "%"), 4)