Largest of four numbers | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

20th Feb 2021, 5:49 PM
Ibrahim Yusuf
Ibrahim Yusuf - avatar
3 Answers
+ 2
visph thank you, ot works. Buy O don't really understand the sort callback function
20th Feb 2021, 6:03 PM
Ibrahim Yusuf
Ibrahim Yusuf - avatar
+ 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);
20th Feb 2021, 5:56 PM
visph
visph - avatar
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
20th Feb 2021, 6:07 PM
visph
visph - avatar