+ 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].
2 Respuestas
+ 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
+ 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.