Basic Algorithm Advice - Return Largest Number From Array

Basic Algorithm Advice - Return Largest Number From Array
0.0 0

#1

Hello,

I was working on the basic algorithm challenge in the title and I want to know how it can be best completed and if you can finish it off with my code. I imagine it would be better to use map.() reduce() filter() functions but I do not understand how to use them yet.

function largestOfFour(arr) {
// You can do this!
var finalArr = [[], [], [], []];
var highestNum = 0;
var index = 0;
var stringToArray = “”;
do {
for (i=0;i<arr.length;i++)
{
for (j=0; j<arr[i].length; j++)
{
if (arr[i][j] > highestNum)
{
highestNum = arr[i][j];
finalArr[index][0] = highestNum;
}
}
index++;
}
} while (index < finalArr.length);

for (i=0; i<finalArr.length; i++)
{
for (j=0; j<finalArr[i].length; j++)
{
stringToArray += finalArr[i][j] + “,”;
}
}

var arrayToString = stringToArray.split(",");
arrayToString.pop();

for (i=0;i<arrayToString.length;i++)
{
for (k=0; k<finalArr.length;k++)
{
for (l=0; l<finalArr[k].length; l++)
{
finalArr[k][l] = arrayToString[i];
}
}
}

return finalArr;
}

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


#2

always provide the link to the challenge so that we can test and see which solution works best.


#3

#4

no there is a better way. it very simple and all you have to do is this make the first number a var. so var i[0]. then loop through the array. if one number is larger than the next it becomes that var. so if i[2] > i[0] i[2] becomes that var. `

  for(var i = 1;i < arry.length;i++){
         if(max < arry[i].length){
         max = arry[i].length;
       }


   }
   
      return max;
    
}

`