Repository navigation
#204 Additive Inverse
Adam Kubiś edited this page Oct 28, 2026
·
1 revision
A number added with its additive inverse equals zero. Create a function that returns an array of additive inverses.
Current source and tests | Catalog | Testing and tooling
From the cloned repository root, after installing dependencies:
node "Edabit/204 Additive Inverse.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.
//A number added with its additive inverse equals zero. Create a function that returns an array of additive inverses.
function additiveInverse(arr) {
return arr.map(x => -x);
}
import { assertEquals } from '../testHelper.js';
assertEquals(additiveInverse([5, -7, 8, 3]), [-5, 7, -8, -3])
assertEquals(additiveInverse([1, 1, 1, 1, 1]), [-1, -1, -1, -1, -1])
assertEquals(additiveInverse([-5, -25, 35]), [5, 25, -35])