Skip to content

Instantly share code, notes, and snippets.

@GreggSetzer
Created July 16, 2018 15: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 GreggSetzer/207a7b7e98828160a5ed74310547216d to your computer and use it in GitHub Desktop.
Save GreggSetzer/207a7b7e98828160a5ed74310547216d to your computer and use it in GitHub Desktop.
JavaScript Interview Question: Largest integer in array?
/*
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