-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsub-array-elements-sum.js
More file actions
28 lines (20 loc) · 4.1 KB
/
sub-array-elements-sum.js
File metadata and controls
28 lines (20 loc) · 4.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// Given an array (or list or vector) of arrays (or, guess what, lists or vectors) of integers,
// your goal is to return the sum of a specific set of numbers,
// starting with elements whose position is equal to the main array length and going down by one at
// each step.
// Say for example the parent array (etc, etc) has 3 sub-arrays inside: you should consider the
// third element of the first sub-array, the second of the second array and the first element in
// the third one: [[3, 2, 1, 0], [4, 6, 5, 3, 2], [9, 8, 7, 4]] would have you considering 1 for
// [3, 2, 1, 0], 6 for [4, 6, 5, 3, 2] and 9 for [9, 8, 7, 4], which sums up to 16.
// One small note is that not always each sub-array will have enough elements, in which case you
// should then use a default value (if provided) or 0 (if not provided), as shown in the test cases.
function elementsSum(arr, defaultVal = 0){
return arr.reduce((acc, cur, index) => {
const indexToAdd = Math.abs(index - (arr.length - 1));
const valueToAdd = cur[indexToAdd] === undefined ? defaultVal : cur[indexToAdd];
return acc + valueToAdd;
}, 0);
};
result = elementsSum([[-79, -77, -307, 97, 919, 0, 10, 45, -651, -86, 659, 1, 28, 17, 37, 92, -569, 420, 3, -727, 0, 483, 970, -70, 1, 48], [-294, -99, -99, 26, -5, 0, -30, 80], [-23, -19, -628, -934, 72, -436, 834, 4, -203, 323, -880, 0, 479, 0, 500, 364, 111, -76, -1, -62, 306, 1], [12, 6, 0, -68, -7, -357, -495, 3, 81, -252, 900, -569, 73, -31, -3, -552, 41, -2, 0, 30, 316, -1], [-19, -733, 46, -517, -1, -626, -112, -7, -902, -318, -6, -808, -971, -1, 2, 8], [-744, 51, 18, 522, -95, -1, -10, 318, 824, 48, 734, 10, -458, -433, 34, 0, -513, -645, 1, 4, 2], [57, -96, -58, 967, 1, 0, -75, 527, -474, -55, 638, -100, -66, 8, 833, -73, 311, -796, 232, 19, -31, -687, -2, 978, 12], [1, -405, -2, -49, 373, -1, -912, 130, -1], [-7, -1, -29, 177], [-430, -1, 592, 1, -805, 65, -57, -916, 1, -585, 4, -10, 644, 14], [-1, 8, 2, -160, -309, 465, 7, 679, -74, 3, -66, 827, -6, 1, -46, -237, 10, -7, 6, -10, -5, 31, 1, 95, 1, 9, 9, 333, -9], [230, 557, 598, -4, -480, -360, 44, -4, 133, 280, -348, 814, -242, -1, -34, 28, 2], [83, 4, 83, 0, -6, 59, -153, -587, -803, -110, -81, -32, -73, 384, 57, -9, -883, 50, 0, 1, -11, 40, 631, 804, 506, -20, 532, 678, -1, 973, 34, -90, 884, -6, -12, 0], [-11, -10, 1, 395, -640, -9, 32, -68, 617, 563, -3, -7, 28, 390, 82, 686], [-9, 7, -3, 49, -302, -370, -3, 216, 24, -29, 9, 36, -8, -5, 77, 205, 324, 3, 98, 375, -608, 8, -10, -739, 713, 1, 340, -30, -51, 9, 288, 876, 212, 574, -94, 39, 78], [-5, 941, -56, -22, -10, -1, 31, 71, -98, -1, 44, 1, 4, 340, 3, -87, 246]], -3);
console.log(result);
// Testing for [[-79, -77, -307, 97, 919, 0, 10, 45, -651, -86, 659, 1, 28, 17, 37, 92, -569, 420, 3, -727, 0, 483, 970, -70, 1, 48], [-294, -99, -99, 26, -5, 0, -30, 80], [-23, -19, -628, -934, 72, -436, 834, 4, -203, 323, -880, 0, 479, 0, 500, 364, 111, -76, -1, -62, 306, 1], [12, 6, 0, -68, -7, -357, -495, 3, 81, -252, 900, -569, 73, -31, -3, -552, 41, -2, 0, 30, 316, -1], [-19, -733, 46, -517, -1, -626, -112, -7, -902, -318, -6, -808, -971, -1, 2, 8], [-744, 51, 18, 522, -95, -1, -10, 318, 824, 48, 734, 10, -458, -433, 34, 0, -513, -645, 1, 4, 2], [57, -96, -58, 967, 1, 0, -75, 527, -474, -55, 638, -100, -66, 8, 833, -73, 311, -796, 232, 19, -31, -687, -2, 978, 12], [1, -405, -2, -49, 373, -1, -912, 130, -1], [-7, -1, -29, 177], [-430, -1, 592, 1, -805, 65, -57, -916, 1, -585, 4, -10, 644, 14], [-1, 8, 2, -160, -309, 465, 7, 679, -74, 3, -66, 827, -6, 1, -46, -237, 10, -7, 6, -10, -5, 31, 1, 95, 1, 9, 9, 333, -9], [230, 557, 598, -4, -480, -360, 44, -4, 133, 280, -348, 814, -242, -1, -34, 28, 2], [83, 4, 83, 0, -6, 59, -153, -587, -803, -110, -81, -32, -73, 384, 57, -9, -883, 50, 0, 1, -11, 40, 631, 804, 506, -20, 532, 678, -1, 973, 34, -90, 884, -6, -12, 0], [-11, -10, 1, 395, -640, -9, 32, -68, 617, 563, -3, -7, 28, 390, 82, 686], [-9, 7, -3, 49, -302, -370, -3, 216, 24, -29, 9, 36, -8, -5, 77, 205, 324, 3, 98, 375, -608, 8, -10, -739, 713, 1, 340, -30, -51, 9, 288, 876, 212, 574, -94, 39, 78], [-5, 941, -56, -22, -10, -1, 31, 71, -98, -1, 44, 1, 4, 340, 3, -87, 246]] and -3