| Total Complexity | 7 |
| Complexity/F | 7 |
| Lines of Code | 32 |
| Function Count | 1 |
| Duplicated Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
| 1 | |||
| 2 | const find = (list, search, start = 0, end = -1) => { |
||
| 3 | if (end === -1) { |
||
| 4 | end = list.length - 1; |
||
| 5 | } |
||
| 6 | // put your code here to address problems |
||
| 7 | if (start === end) { |
||
| 8 | if (list[start] === search) { |
||
| 9 | return start; |
||
| 10 | } |
||
| 11 | //not found |
||
| 12 | return -1; |
||
| 13 | } |
||
| 14 | let mid = parseInt((end + start) / 2); |
||
| 15 | |||
| 16 | if (list[start] < list[end]) { |
||
| 17 | // sorted sublist |
||
| 18 | if (list[start] <= search && search <= list[mid]) { |
||
| 19 | return find(list, search, start, mid); |
||
| 20 | } |
||
| 21 | return find(list, search, mid + 1, end); |
||
| 22 | } |
||
| 23 | // un-order sublist |
||
| 24 | let check = find(list, search, start, mid); |
||
| 25 | if (-1 === check) { |
||
| 26 | check = find(list, search, mid + 1, end) |
||
| 27 | } |
||
| 28 | return check; |
||
| 29 | |||
| 30 | } |
||
| 31 | |||
| 32 | |||
| 33 | module.exports = find; |
||
| 34 |