I am trying to get the largest number of each sub-array in the array I sorted them but I can't seem to get my result. The result should be an array of the largest number of each sub-array.
function largestOfFour(arr) {
for(var i = 0; i < arr.length; i+=1){
for(var n = 0; n < arr[i].length; n+=1 ){
arr[n].sort(function(a, b){return b-a});
}
console.log(arr[i][0]);
}
return arr[i][0];
}
largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
6 Answers 6
Try something like this. You do not need the second loop, also create a temp array and put the values there.
function largestOfFour(arr) {
var tmparr = [];
for(var i = 0; i < arr.length; i+=1){
//for(var n = 0; n < arr[i].length; n+=1 ){
arr[i].sort(function(a, b){return b-a});
// }
tmparr.push(arr[i][0]);
}
console.log(tmparr)
return tmparr;
}
largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
function largestInArrays(... arrays) {
return arrays.map(a => Math.max(...a));
}
console.log(largestInArrays([4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]));
This works because Math.max
combined with the ...
argument trick will return the largest numerical value within an array and .map
allows you to create a new array from the old array by performing an operation on each element of the array.
-
1While I added an upvote to this answer, you don't really need the ellipsis (...) in the function definition, you only need it in the return line. This answer is simple and efficient.Sunday Ikpe– Sunday Ikpe2020年12月27日 05:06:29 +00:00Commented Dec 27, 2020 at 5:06
-
@SundayIkpe The reason the ellipsis are in the function definition is because I want all of the arguments to be treated as a single array (so the outer brackets aren't necessary when calling the function). Hence the name change from
largestOfFour
tolargestInArrays
. However looking back at the original question it seems I might have mistaken the specific wanted functionality here.Jonathan Gray– Jonathan Gray2020年12月27日 05:08:05 +00:00Commented Dec 27, 2020 at 5:08
You only have to loop through the array once and create a temporary array to store the largest numbers. Using Math.max()
allows you to get the largest number within each array. There's no need to sort the arrays when Math.max()
will already find the largest number for us.
function largestOfFour(arr) {
let largestNumbers = []
arr.forEach(innerArr => {
let largestNumberOfInnerArray = (Math.max(...innerArr))
largestNumbers.push(largestNumberOfInnerArray)
});
return largestNumbers
}
console.log(largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]));
-
1Thanks alot. This worked, but after i added var, like this: var largestNumbers = [];Okiemute Gold– Okiemute Gold2020年12月27日 04:56:33 +00:00Commented Dec 27, 2020 at 4:56
-
Thanks for catching that. I've updated the postCaleb Mabry– Caleb Mabry2020年12月27日 04:59:53 +00:00Commented Dec 27, 2020 at 4:59
function largestOfFour(arr) {
const largestNumb = [];
// loop through the array
for(var i = 0; i < arr.length; i+=1){
// loop through each sub-array (of grabbed array), and sort the numbers in descending order.
arr[i].sort(function(a, b){return b-a});
console.log(arr[i][0]);
// save each of the first value (largest num) in an array
largestNumb.push(arr[i][0]);
}
// console.log(largestNumb);
return largestNumb;
}
You could use underscore.js to achieve this:
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.12.0/underscore-min.js"></script>
<script>
function largestOfFour(arr){
return arr.map(a => _.max(a));
}
</script>
Tryining
largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
your result will be:
[5, 27, 39, 1001]
-
Regular sort does a string comparison not a numerical comparisonJonathan Gray– Jonathan Gray2020年12月27日 04:38:45 +00:00Commented Dec 27, 2020 at 4:38
-
Yea, I have updated with underscoreJS. That way it behaves the way you want. Its the simplest answer I could think of.Sunday Ikpe– Sunday Ikpe2020年12月27日 04:51:27 +00:00Commented Dec 27, 2020 at 4:51
Functions in javascript have variables sometimes. A variable saves a value to use whenever coders want. But functions have specific scopes so variables can be defined twice in a function. For example
let food = 'banana';
console.log(food) // it shows 'banana'
function digestion() {
let food = 'poop'
console.log(food) // it shows 'poop'
}
console.log(food) // it still shows 'banana'
So scopes can define values differently. But in largestOfFour
function arr[i][0]
seems out of for
loof. Seems there is a little mistake. Without the line the function works.
function largestOfFour(arr) {
for(var i = 0; i < arr.length; i+=1){
for(var n = 0; n < arr[i].length; n+=1 ){
arr[n].sort(function(a, b){return b-a});
}
console.log(arr[i][0]);
}
//return arr[i][0];
}
largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);