Partical Sums Task
Write function 'getParticalSums' that takes numeric range as first argument and a value stub if value isn't a number as second and returns array of partical sums.
by evgkch
JavaScript
/**
* Write a function that takes numeric range [a(0), a(1), ... , a(n)]
* as first argument and a stub as second
* and returns a range of partical sums:
* a(i) = a(0) + a(1) + ... + a(i),
* exept not numeric a(j) values that equals the stub
**/
function getParticalSums(range, stub = null) { /* ... */ }
test(getParticalSums, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
.equals([0, 1, 3, 6, 10, 15, 21, 28, 36, 45]);
test(getParticalSums, [null, null, null, null, null, null, null, null, null, null], 0)
.equals([0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
test(getParticalSums, [null, 1, null, 3, 4, null, null, null, null, 9])
.equals([null, 1, null, 4, 8, null, null, null, null, 17]);
test(getParticalSums, [0, null, 2, 3, null, 5, 6, 7, 8, null])
.equals([0, null, 2, 5, null, 10, 16, 23, 31, null]);
function test(fn, ...args) {
const v = fn.call(fn, ...args);
const vLength = v.length;
return {
equals: (result) => {
let passed = true;
for (let i = 0; i < vLength; i++)
passed = passed && (v[i] == result[i]);
console.log(passed ? 'Test passed! Congrats!' : 'Oh... Test failed!');
}
};
}