+ 2
Largest of four numbers
This is a free code camp challenge. Return an array consisting of the largest number from each provided sub-array. example: largestOfFour([[13, 27, 18, 26], [4, 5, 1, 3], [32, 35, 37, 39], [1000, 1001, 857, 1]]) should return [27, 5, 39, 1001]. can someone explain what is wrong with my algorithm? https://code.sololearn.com/W76V8owgmW54/?ref=app
3 Respuestas
+ 2
visph thank you, ot works. Buy O don't really understand the sort callback function
+ 1
js array sort method default to alphanumeric sort... you must provide a sort callback to compare values as numbers:
ele = ele.sort((a,b) => a-b);
0
comparison function should receive two array items as argument, and expect a numeric value to be returned (negative i a < b, 0 if a == b, positive if a > b):
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort