Repository navigation
#147 Many Operators
Adam Kubiś edited this page Oct 28, 2026
·
1 revision
Some basic arithmetic operators are +, -, *, /, and %.
In this challenge you will be given three parameters, num1, num2, and an operator.Use the operator on number 1 and 2.
Current source and tests | Catalog | Testing and tooling
From the cloned repository root, after installing dependencies:
node "Edabit/147 Many Operators.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.
//Some basic arithmetic operators are +, -, *, /, and %.
//In this challenge you will be given three parameters, num1, num2, and an operator.Use the operator on number 1 and 2.
function operate(num1, num2, operator) {
switch (operator) {
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(operate(1, 1, "+"), 2)
assertEquals(operate(1, 1, "-"), 0)
assertEquals(operate(1, 1, "*"), 1)
assertEquals(operate(1, 1, "/"), 1)
assertEquals(operate(1, 1, "%"), 0)
assertEquals(operate(2, 1, "+"), 3)
assertEquals(operate(2, 1, "-"), 1)
assertEquals(operate(2, 1, "*"), 2)
assertEquals(operate(2, 1, "/"), 2)
assertEquals(operate(2, 1, "%"), 0)