Help With FCC Problem - Return Largest Numbers in Arrays

The task is to return the highest number from each sub-array and put them all into one array.

I have all of the acquired numbers, but I have an extra member that is empty in my array for some reason. I’ve tried troubleshooting it, but I can’t find the solution.

function largestOfFour(arr) { var high = ''; for(var i = 0 ; i < arr.length ; i++) { var find = Math.max(...arr[i]); high = high + find + " "; } return high.split(' '); } largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);

Result: [“5”, “27”, “39”, “1001”, “”]

Where I would start looking for a solution to your problem is at the high = high + find + " " bit, namely the space you’re adding at the end.

Since this runs on every loop iteration, that means it’s adding a space at the very end of the final string you’re then splitting. And since you’re splitting the string on every space, then basically what you have is four numbers and then an empty element (there’s nothing after the last space, but it’s still going to split it, so it gives you an empty string). Hope this makes sense, let me know if any other questions.

Oh thanks, I see now.

I tried using Array.prototype.pop() to try to remove that last part of the array, but removed the whole array instead.

What I would do is create the high variable as an empty array instead of a string. When you find the highest from each group, you can add it to the high array and then just return the new array. It simplifies it so it’s easier to follow and you don’t have to deal with the empty value inside the array.

1 Like

your variable “find” should be

var find=Math.max.apply(Math, arr[i])
var high="" //this is the problem, you dont need it

then you can push every value into a new array