Repository navigation
#130 Minimal2
Write a function that returns the boolean true if the given number is zero,
the string "positive" if the number is greater than zero or the string "negative" if it's smaller than zero.
Current source and tests | Catalog | Testing and tooling
From the cloned repository root, after installing dependencies:
node "Edabit/130 Minimal2.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.
The mixed return types are part of this exercise's contract and are declared explicitly in JSDoc. Boolean outputs remain booleans; they are not converted to strings to satisfy a static-analysis rule.
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.
//Write a function that returns the boolean true if the given number is zero,
//the string "positive" if the number is greater than zero or the string "negative" if it's smaller than zero.
/**
* @param {number} x Number to classify.
* @returns {true | "positive" | "negative"} The mixed result types required by the exercise.
*/
function equilibrium(x) {
if (x === 0) return true;
if (x > 0) return "positive";
return "negative";
}
import { assertEquals } from '../testHelper.js';
assertEquals(equilibrium(0.000), true)
assertEquals(equilibrium(0), true)
assertEquals(equilibrium(-0), true)
assertEquals(equilibrium(3), "positive")
assertEquals(equilibrium(66), "positive")
assertEquals(equilibrium(5425), "positive")
assertEquals(equilibrium(1111.1), "positive")
assertEquals(equilibrium(0.0001), "positive")
assertEquals(equilibrium(Number.MIN_VALUE), "positive")
assertEquals(equilibrium(-3), "negative")
assertEquals(equilibrium(-66), "negative")
assertEquals(equilibrium(-5425), "negative")
assertEquals(equilibrium(-1111.1), "negative")
assertEquals(equilibrium(-0.0001), "negative")
assertEquals(equilibrium(-Number.MIN_VALUE), "negative")