Skip to content

Instantly share code, notes, and snippets.

@denismcdonald
Created May 24, 2018 01:59
Show Gist options
  • Save denismcdonald/8ce651315374cd8c39618ddfc1f4c89b to your computer and use it in GitHub Desktop.
Save denismcdonald/8ce651315374cd8c39618ddfc1f4c89b to your computer and use it in GitHub Desktop.
Return largest numbers from sub-arrays (JavaScript)
function returnLargest(arr) {
var newArr = [];
for (var i = 0; i < arr.length; i++) {
var subArray = arr[i];
var largest = arr[i][0];
for (var j = 0; j < arr[i].length; j++) {
if (subArray[j] > largest) {
largest = subArray[j];
}
}
newArr.push(largest);
}
return newArr;
}
returnLargest([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment