Created
January 29, 2020 17:03
-
-
Save danielwayota/907d2b609353099b38f501c51871f167 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let data = [4,9,13,102,103,101,25,22,21,20,18,9,1] | |
| function min(data) { | |
| return Math.min(data[0], data[data.length-1]) | |
| } | |
| function max(data, index) { | |
| index = !index ? Math.floor(data.length / 2) : index | |
| let left = data[index-1] | |
| let right = data[index+1] | |
| let current = data[index] | |
| if (current > left && current > right) { | |
| return current | |
| } | |
| if (left > right) { | |
| return max(data, index - Math.floor(index / 2)) | |
| } else { | |
| return max(data, index + Math.floor(index / 2)) | |
| } | |
| } | |
| console.log(max(data)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment