0

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.

1
  • Share the structure of the arr array you're passing to the function, otherwise you'll add a layer of complexity to your question: people have to suppose what you're trying to do before providing any help. Commented Dec 31, 2017 at 2:23

4 Answers 4

1
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;
}
Sign up to request clarification or add additional context in comments.

Comments

0

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;
}

Comments

0

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?

2 Comments

Oh, that took me for ever, and I didn't notice those other posts. Those look good too.
Yes, I corrected my question. Thank you for explaining. It makes much more sense with looping once and just pushing with segregatedArr[0]
0
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));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.