| Total Complexity | 5 |
| Complexity/F | 5 |
| Lines of Code | 25 |
| Function Count | 1 |
| Duplicated Lines | 0 |
| Ratio | 0 % |
| Changes | 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; |