# Problem with Basic Algorithm Scripting: Return Largest Numbers in Arrays

Problem with Basic Algorithm Scripting: Return Largest Numbers in Arrays
0

#1

I fail to see why my code doesn´t solve the puzzle:

``````function largestOfFour(arr) {
var largestArray=[];

for (var i=0; i < arr.length; i++){
var largestNumber=0;
for (var j=0; j<arr.length; j++){
if (largestNumber < arr[i][j]){
largestNumber=arr[i][j];
}
}
largestArray.push(largestNumber);
}
return largestArray;
}

largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
``````

User Agent is: `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36`.

#2

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 easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (`</>`) will also add backticks around text.

Note: Backticks are not single quotes.

#3

See if you can spot the error in this line:

``````  for (var j=0; j<arr.length; j++){
``````

#4

Thanks for the reply. I really can´t, though. Am I supposed to change arr.length to arr[i].length in the second for loop? Because that doesn´t seem to work either.

#5

if you console.log the test you are failing
`console.log(largestOfFour([[17, 23, 25, 12], [25, 7, 34, 48], [4, -10, 18, 21], [-72, -3, -17, -10]]))`
it returns
`25,48,21,0`

but the largest number in that last array is -3 not 0. Why is it saying the largest number is 0?

#6

Thanks for the reply. I get what you are saying: it isn´t working very well with negative numbers. But I don´t really know how to correct it. Is it normal to struggle like this with these exercises?

#7

nah its all good, coding can be a bit of a steep learning curve when you get to data structures and algorithms, it is common to have some struggles.

look at line 5 of your code
`var largestNumber=0;`

you set the largest number as 0 and all the numbers in the negative array are less than 0

#8

I set it to [ ] but still won´t budge. Feel so dumb, lol.

#9

You don’t want to set the `largestNumber` to an Array, you were on the right track before.

`var largestNumber=0;`

I suggest you get a piece of paper and go through your algorithm step by step writing down the changes to variables as they happen.

You should find that when you get to the negative array it is comparing

``````if (largestNumber < arr[i][j]){
largestNumber=arr[i][j];
}
``````

Remember your `largestNumber = 0`, which is greater than all the negative numbers

#10

Thanks for all the help. I get what you are saying: I should either construct an “else” statement to cover this scenario, or use a different comparison operator. No idea which, though.

#11

instead of setting the default `largestNumber` as 0 you could try set it to the first value in the array.

another way would be to set `largestNumber` to -9999 or some large negative number but this wouldn’t work for all cases

#12

That did the trick. Thank you so much!