+ 4

somebody please tell me what is the problem with this code ,it is a code to find the largest of the four arrays.I have mentioned

var farray = []; var ans = []; function largestOfFour(arr) { for (i=0;i<arr.length;i++){ farray.push(arr[i].sort()); ans.push(farray[i][3]); } return ans; } largestOfFour(([[4, 9, 1, 3], [13, 35, 18, 26], [32, 35, 97, 39], [1000000, 1001, 857, 1]])); Tests to satisfy:- largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]) should return an array. largestOfFour([[13, 27, 18, 26], [4, 5, 1, 3], [32, 35, 37, 39], [1000, 1001, 857, 1]]) should return [27,5,39,1001]. largestOfFour([[4, 9, 1, 3], [13, 35, 18, 26], [32, 35, 97, 39], [1000000, 1001, 857, 1]]) should return [9, 35, 97, 1000000].

6th May 2018, 11:40 AM
Hemant Malik
Hemant Malik - avatar
2 Antworten
+ 3
These posts explains the issue and offer ways to solve it. For a quick answer stick to the first. The second post is the docs of the JS sort() method; it offers valuable information. Google is your friend. https://stackoverflow.com/questions/1063007/how-to-sort-an-array-of-integers-correctly https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort
6th May 2018, 3:56 PM
cyk
cyk - avatar
+ 3
cyk thanx,that is an interesting discussion u linked here, found the problem and now its executing the way i wanted it to be.
8th May 2018, 2:33 PM
Hemant Malik
Hemant Malik - avatar