Monday, February 29, 2016

RETURN LARGEST NUMBERS IN ARRAY

Challenge here was to return an array consisting of the largest number from each provided sub-array. The provided array contained exactly 4 sub-arrays.

For example: 

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

An array is a container object that holds a fixed number of values of a single type. In our e.g array is array of arrays.

Initial I misunderstood the challenge. I thought that the challenge requires to return the Array that is having maximum value number in it. I struggled whole of the day until I reexamined the challenge.

The requirement here was to make an Array from this Array of Array, that contained only the greatest value in each sub-Array.

Then on it was a cake walk for me because I already struggled a lot through out the day.

So, here is my solution:

function largestOfFour(arr) {     var arr1 =[];     for (var i = 0; i < arr.length; i++ ){         a = arr[i];         var max = 0;                  for (var j = 0; j < a.length; j++){             var b = a[j];             if(b > max){                 max = b;             }        }    arr1.push(max);   } return arr1; }

1 comment: