topics/distance/index.js   A
last analyzed

Complexity

Total Complexity 5
Complexity/F 5

Size

Lines of Code 25
Function Count 1

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 14
mnd 4
bc 4
fnc 1
dl 0
loc 25
rs 10
bpm 4
cpm 5
noi 0
c 0
b 0
f 0
1
const maxDistance = (arr) => {
2
    // put your code here to address problems
3
    if (arr.length < 2) {
4
        return -1;
5
    }
6
7
    // keep the minimum element
8
    let min = arr[0];
9
    // keep maxDistanceValue
10
    let maxDistanceValue = -1;
11
12
    for (let i = 1; i < arr.length; i++) {
13
        if (arr[i] < min) {
14
            min = arr[i];
15
            continue;
16
        }
17
        let distance = (arr[i] - min);
18
        if (distance > maxDistanceValue) {
19
            maxDistanceValue = distance;
20
        }
21
    }
22
    return maxDistanceValue;
23
}
24
25
module.exports = maxDistance;