-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsumOfDifferencesInArray.js
More file actions
33 lines (26 loc) · 1.04 KB
/
sumOfDifferencesInArray.js
File metadata and controls
33 lines (26 loc) · 1.04 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
29
30
31
32
33
//Instructions:
// Your task is to sum the differences between consecutive pairs in the array in descending order.
function sumOfDifferences(arr) {
// set global variable for array container
let emptyArr = [];
// check if array contains at least one element
if (arr.length > 1) {
// sort array in desc order
const sortedArr = arr.reverse()
// run a subtraction for each pair in the array
for (let i = 0; i <= sortedArr.length-2; i++) {
const item = sortedArr[i];
const itemNeighbor = sortedArr[i + 1];
const subtractedPair = item - itemNeighbor;
emptyArr.push(subtractedPair);
}
// add the result of the subtraction
const addedPairs = emptyArr.reduce(function (previousValue, currentValue) {
return previousValue + currentValue
}, 0)
//convert from array to num
const finalNum = parseInt(addedPairs);
//return final num
return finalNum;
} else return 0;
}