Repository navigation
#191 Invert an Array
Adam Kubiś edited this page Oct 28, 2026
·
1 revision
Create a function that takes an array of numbers arr and returns an inverted array.
Current source and tests | Catalog | Testing and tooling
From the cloned repository root, after installing dependencies:
node "Edabit/191 Invert an Array.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 an array of numbers arr and returns an inverted array.
function invertArray(arr) {
return arr.map((x) => x === 0 ? 0 : -x);
}
import { assertEquals } from '../testHelper.js';
assertEquals(invertArray([1, 2, 3, 4, 5]), [-1, -2, -3, -4, -5])
assertEquals(invertArray([1, -2, 3, -4, 5]), [-1, 2, -3, 4, -5])
assertEquals(invertArray([]), [])
assertEquals(invertArray([0]), [0])