Created
July 16, 2018 15:51
-
-
Save GreggSetzer/207a7b7e98828160a5ed74310547216d to your computer and use it in GitHub Desktop.
JavaScript Interview Question: Largest integer in array?
This file contains 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
/* | |
Return the largest value in an array of non-negative integers | |
*/ | |
function largestInt(arr) { | |
if (arr.length === 0) { | |
return -1; | |
} | |
return arr.reduce(comparator); | |
} | |
const comparator = ((largestValue, element) => largestValue > element ? largestValue : element); | |
console.log(largestInt([1,2,3,4,5,3,2,1])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment