0

JS Function:

function largestOfFour(arr) {


var biggest = 0;
var newArray = [];

for(var i = 0; i < arr.sort().reverse().length; i++){
  for(var j = 0; j < arr[i].length; j++){

    if(biggest < arr[i][j]){
      biggest = arr[i][j]; // problem after this
      //newArray.push(biggest);  this will add the first element in each sub-array
      // result [4,5,13,27,32,35,37,39,1000,1001]
    }

  }
}

return newArray;

}

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


//  Expected Output:

// [5, 27, 39, 1001]

I am having a hard time with this. How would I add or change anything to what I have to get the biggest number from each sub-array and add that number to newArray using for-loops?

3
  • @Seiyria I would like to do this using for loops Commented Jan 31, 2017 at 15:55
  • add 'newArray.push (biggest)' after your second loop inside the first loop Commented Jan 31, 2017 at 15:57
  • for(var i = 0; i < arr.length; i++) { newArray.push(Math.max.apply(null, arr[i])); } Commented Jan 31, 2017 at 16:02

6 Answers 6

4

Since you'd like to use a for loop:

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

What this will do is sort the inner arrays and grab the first one out of there.

Sign up to request clarification or add additional context in comments.

2 Comments

you could save a little time by doing b-a in the comparator and skip the reverse()
Sure. I'll make that change. I forgot JS doesn't sort arrays of numbers numerically so I just sorta patched that in, thanks.
3

You can use map() instead of for loops.

var arr = [[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]

function arrMax(data) {
  return data.map(e => Math.max.apply(null, e))
}
console.log(arrMax(arr))

4 Comments

In my question I stated that I would like to use for loops
Yeah, why use a ferrari if you can have a lada... @BviLLe_Kid
@baao I know that this answer is probably the better answer.. but this is just to test myself with loops
@BviLLe_Kid then for loop over the arr, then use Math.max.apply(null, arr[i]) on the sub arrays. However, I'd argue .map would be the preferable approach since it returns out a new array.
1

You could use the spread syntax for Math.max.

function arrMax(data) {
    return data.map(a => Math.max(...a))
}

var arr = [[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]

console.log(arrMax(arr));

Comments

0

Maybe not the best but this should work

function largestOfFour(series) {
   return series.map(function (serie) {
      return Math.max.apply(null, serie);
      // return Math.max(...serie);
   });
}

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

1 Comment

You can't just do .sort(), you need to pass in a comparator when dealing with numbers. If you try this code, the last array will return 857 as the highest number, which is not correct.
0

For very large sizes of arrays and sub array items i guess this would be the most efficient..

var arr = [[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]],
    res = arr.map(a => a.reduce((p,c) => p > c ? p : c));
console.log(res);

Comments

0

I did it like this, it works with the negative numbers as well as with positives

function largestOfFour(arr) {
  var maxvalarr = [];
  for(let i = 0; i < arr.length ; i++){
    let maxvalue = -10000000;
    for(let g = 0; g < arr[i].length; g++)
      if(arr[i][g] > maxvalue)
         maxvalue = arr[i][g];
      
    maxvalarr[i] = maxvalue;
  }
console.log(maxvalarr)

  return maxvalarr;
}

largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
largestOfFour([[4, 9, 1, 3], [13, 35, 18, 26], [32, 35, 97, 39], [1000000, 1001, 857, 1]]);
largestOfFour([[17, 23, 25, 12], [25, 7, 34, 48], [4, -10, 18, 21], [-72, -3, -17, -10]])

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.