2

I've been trying to solve this practice problem:

Return an array consisting of the largest number from each provided sub-array. For simplicity, the provided array will contain exactly 4 sub-arrays.

But my code only returns the single element of from the whole array, if I unshift all the max elements it produces totally wrong results, I tried to execute the nested loop separately and it worked fine but it creates a problem when combined with outer loop.

function largestOfFour(arr)
{
    // You can do this!
    var max = 0;
    var largestArray =[];
    for (var i = 0; i <4; i++)
    {
        for (var j = 0; j <4; j++)
        {
            if (arr[i][j]>max)
            {
              max=arr[i][j];
              largestArray.unshift(max);
              //console.log(max);
            }

        }
    }
      console.log(largestArray);
    return max;
}

largestOfFour([[4, 5, 1, 13], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
1
  • Before anyone else adds Math.max answers the OP would like help with the code they've written. Commented Oct 2, 2017 at 9:03

3 Answers 3

3

How to fix your code (see comments in code):

function largestOf(arr) {
  var max;
  var largestArray = [];

  for (var i = 0; i < arr.length; i++) { // the arr length is the number of sub arrays
    max = -Infinity; // max should be reinitialized to the lowest number on each loop
    for (var j = 0; j < arr[i].length; j++) { // the length is the number of items in the sub array
      if (arr[i][j] > max) { // just update max to a higher number
        max = arr[i][j];
      }
    }
    
    largestArray.push(max); // push max after the internal loop is done, and max is known
  }

  return largestArray; // return the largest array
}

var result = largestOf([[4, 5, 1, 13], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);

console.log(result);

Another solution is to use Array#map, and apply Math#max to each sub array to get it's maximum value:

function largestOf(arr) {
  return arr.map(function(s) {
    return Math.max.apply(Math, s);
  });
}

var result = largestOf([[4, 5, 1, 13], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);

console.log(result);

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

Comments

1

You can do

function largestOfFour(arr)
{
    return arr.map(e => Math.max(...e));
}

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

console.log(result);

3 Comments

I worked fine, but can you please figure-out whats wrong with my code ?
I saw a very interesting format '...e' can you explain about it?
0

Hopefully this helps:

var pre = onload, largestOfFour; // for use on other loads
onload = function(){
if(pre)pre(); // change var pre name if using technique on other pages

function largestOfFour(arrayOfFours){
  for(var i=0,a=[],l=arrayOfFours.length; i<l; i++){
    a.push(Math.max.apply(null, arrayOfFours[i]));
  }
  return a;
}
console.log(largestOfFour([[4, 5, 1, 13], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]));

}

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.