(array, n)
| 1 | function leastLarger (array, n) { |
| 2 | const larger = []; |
| 3 | for (let i = 0; i < array.length; i++) { |
| 4 | if (array[i] > array[n]) { |
| 5 | larger.push(array[i]); |
| 6 | } |
| 7 | } |
| 8 | const min = Math.min(...larger); |
| 9 | return array.indexOf(min); |
| 10 | |
| 11 | //larger contains the elements of array which are larger then array[n] |
| 12 | //min is the minimum of larger's elements |
| 13 | //the function returns with the min's index in array |
| 14 | } |
| 15 | |
| 16 | console.log(leastLarger([-1, -6, -4, -7, 9, -3, 6, 0], 2)); |
| 17 |
no test coverage detected