LickingFilth
LickingFilth

Reputation: 303

Using sort() to properly return largest numbers in arrays

I'm trying to return the largest number in every array to one array with sort() method. I think I did the whole code correctly except sorting:

function largestOfFour(arr) {
    let result=[];
    for(let i=0; i < arr.length; i++) {
        for(let j=0; j < arr[i].length; j++) {
            let segregatedArr = arr[0][j].sort((a,b)=>b-a);
        }
        result = segregatedArr[0][i][0];
    }
    return result;
}

largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);

And I'm getting an error:

TypeError: arr[0][j].sort is not a function

I was trying the slice method or mapping to an array with:

result = $.map(arr, function(value, index) { return [value]; });
result.sort().reverse();

But the error was still the same in both cases.

Upvotes: 0

Views: 98

Answers (4)

D. Seah
D. Seah

Reputation: 4592

var array = [
  [1, 2, 3, 4],
  [10, 5, 3, 4],
  [11, 20, 13, 14],
  [1, 2, 3, 40]
];

function maxVal(arr) {
  var max = arr[0];
  arr.forEach(function(item) {
    max = Math.max(item, max);
  });
  return max;
};

function largestOfFour(arr) {
  return arr.map(function(innerArray) {
    return maxVal(innerArray);
  });
}

console.log(largestOfFour(array));

or if you really really want to use sort :-)

function maxVal(arr) {
  arr.sort(function(a, b) {
    if (a === b) {
      return 0;
    }
    return a > b ? -1 : 1;
  });
  return arr[0];
};

function largestOfFour(arr) {
  return arr.map(function(innerArray) {
    return maxVal(innerArray);
  });
}

console.log(largestOfFour(array));

Upvotes: 0

Joseph
Joseph

Reputation: 61

So, I think for a post like this, it would be helpful if you provide a sample of the input and the desired output.

Are you trying to take as input an array of arrays? And then output a single array, where each element has the largest element of the original arrays? The sample input I created was as follows:

var a1 = [1, 2, 3];
var a2 = [5, 19, 7];
var a3 = [199, 198, 104];
var arrs = [a1, a2, a3];
// run the function and view its output:
console.log(largestOfFour(arrs));
// outputs [ 3, 19, 199 ]

If that is what you were going for, then I think you have too many loops (an outer loop with an unneeded inner loop), so that line of code:

let segregatedArr = arr[0][j].sort((a,b)=>b-a);

is accessing a number rather than an array of numbers. I modified the function as follows:

function largestOfFour(arr) {
    let result=[];
    for(let i=0;i<arr.length;i++){
        let segregatedArr=arr[i].sort((a,b)=> b-a);
      result.push(segregatedArr[0])
    }
    return result;
}

So, I removed the inner loop (with the 'j' index), and then the main loop just sorts the i'th array of the input arrays . Is that what you were going for?

Upvotes: 0

darksmurf
darksmurf

Reputation: 3967

function maxArr(arr2D) {
    let result = [];
    for(let i = 0; i < arr2D.length; i++) {
        result[i] = arr2D[i].sort((a, b) => b - a)[0];
    }
    return result;
}

Upvotes: 1

Umair Afzal
Umair Afzal

Reputation: 181

You can use Math.max.apply to get the largest number:

function largestOfFour(array){
   var result = [];
   for(var i=0; i<array.length;++i){
      result.push(Math.max.apply(Math, array[i]));
   }
   return result;
}

Upvotes: 0

Related Questions