Skip to content

Instantly share code, notes, and snippets.

@BetterProgramming
Created June 30, 2020 14:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BetterProgramming/86c6daa07dd919f2c9f3235d7c07430f to your computer and use it in GitHub Desktop.
Save BetterProgramming/86c6daa07dd919f2c9f3235d7c07430f to your computer and use it in GitHub Desktop.
function maxOf(arr) {
// base case: only one element left in arr
if (arr.length === 1) {
return arr[0]
}
// compare first two elements and remove smaller one
if (arr[1] > arr[0]) {
arr.splice(0, 1) // remove arr[0]
} else {
arr.splice(1, 1) // remove arr[1]
}
return maxOf(arr)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment