-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlargestNum.js
35 lines (29 loc) · 839 Bytes
/
largestNum.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// return largest numbers in a 2d array
function largestNum(arr) {
var maxes = [0, 0, 0, 0];
for (var i=0; i<arr.length; i++) {
for (var j=0; j<arr[i].length; j++){
var currentElement = arr[i][j];
if (currentElement >= maxes[i]) {
maxes[i] = currentElement;
}
}
}
return maxes;
/*
var maxes = [];
for (i=0; i<arr.length; i++) {
var tempMax = arr[i][0];
for (j=0; j<arr[i].length; j++){
var currentElement = arr[i][j];
if (currentElement >= tempMax) {
tempMax = currentElement;
}
}
maxes.push(tempMax);
}
return maxes
*/
}
console.log(largestNum([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]));
// [5, 27, 39, 1001]