I am trying to return to larges numbers within a nested array, my solution is only correct if the largest number in a preceding array is smaller than the largest number in the next sub array, otherwise it fail
The placement of your creation of let biggestInArr=0; - you are creating it outside of the loops so it is always the same variable being used - you either need to find a place to put it so that it will be recreated once for each of the relevant iterations, or you are going to have to reinitialize it each time.
You are initializing your biggestInArr to 0. How will this work with negative numbers? There are two ways to handle this. Either set your initialization to a number that is so small that no other number can be smaller. JS has constants for both the smallest number the system can handle and for the smallest number theoretically imaginable. The other option would be to initialize your variable to something like null. The problem is that your comparison will always be false for null so you would have to invert the logic in your ternary.
there are three ways I would say: you set it to a number in the array, and you just compare it to the other numbers in the array, reducing by one iteration also
what’s your whole code?, as it is your snipped does nothing
I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.
You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.
you never change the value of biggestInArr, so it’s undefined as determined by the line in which it is declared
I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.
You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.
Congrats on solving the problem! If you are interested in an alternative solution only using one for loop you can look at this one with comments explaining code.
//Math.max returns the largest number of each sub array
function largestOfFour(arr) {
let newArr = []
for(let i=0; i<arr.length; i++){
newArr.push(Math.max(...arr[i])) // this is spread syntax.
// Copies the sub array and find max of each sub array and push results to newArr.
}
return newArr;
}
largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
As you can see there are many ways to solve this problem.
Also…consider the expected behavior for deeply nested arrays…do you remove arrays and add them to the top level? Do you flatten the array? What do you do in your recursion ? Just food for thought.
You still need to do a size comparison. But you have exploit the fact that comparing to an undefined will always return false. So, for the false case, you want to set your variable to the currently tested value from the array.