Repository navigation
#230 Absolute Sum
Adam Kubiś edited this page Oct 28, 2026
·
1 revision
Take an array of integers (positive or negative or both) and return the sum of the absolute value of each element.
Current source and tests | Catalog | Testing and tooling
From the cloned repository root, after installing dependencies:
node "Edabit/230 Absolute Sum.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.
//Take an array of integers (positive or negative or both) and return the sum of the absolute value of each element.
function getAbsSum(arr) {
return arr.reduce((acc, val) => acc + Math.abs(val), 0);
}
import { assertEquals } from '../testHelper.js';
assertEquals(getAbsSum([2, -1, -3, 4, 8]), 18);
assertEquals(getAbsSum([-1]), 1);
assertEquals(getAbsSum([-1, -3, -5, -4, -10, 0]), 23);
assertEquals(getAbsSum([8, 9, 10, 32, 101, -10]), 170);
assertEquals(getAbsSum([500]), 500);