-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgettingMAD.js
More file actions
32 lines (23 loc) · 768 Bytes
/
gettingMAD.js
File metadata and controls
32 lines (23 loc) · 768 Bytes
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
let absoluteDifference = (a, b) => {
return Math.abs(b - a);
}
function gettingMad(array) {
let result = [];
for (let i = 0; i < array.length; i++) {
for (let j = i + 1; j < array.length; j++) {
result.push(absoluteDifference(array[i], array[j]));
}
}
if (result.length === 0) {
return 0;
} else {
return Math.min(...result);
}
}
//Parameters: an array of at least two elements, both of which are numbers
//Return value: a positive number like 10, 7, 3, 1
//Example: [-10, 0] => 10 because |-10 - 0| = 10
//Psuedocode:
//for each element, compare it to all the other elements and find the absolute difference
//add each absolute difference to an array
//return the minimum of the array