# Return Largest Numbers in Arrays (understanding how it worksline by line)

this is my code Idk if it works yet but i wan t to go line by line to help understand what is happen here.

``````
function largestOfFour(arr) {
// You can do this!
var count= []

for (var i=0;i<arr.lenght; i++){
var big =0;
for (var si=0;si < arr[i]; si++){
if (arr[i][si]> big){
big= arr[i][si];

}
}
count [i] = big
}

return count;
}

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 6.1; Win64; x64; rv:67.0) Gecko/20100101 Firefox/67.0`.

``````var count = 0
``````

so this one is assigning a variable to count and I want it to count from the first position in the variable which is 0.

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

this one is hard for me to understand. var is assigning i to first position 0. then if i is greater then the length of the array then add one (so should is start with 1 or 0 Idk.

this one is assigning big to 0 inside the loop. why???

this one idk. looks like its assigning si to 0 and if its less then arr after looping the first loop then it counts.
then if its greater than big then it assigns both strings to big. Why???

then it asign the finial count to big.

then it returns count but should that return big?

not working ``````function largestOfFour(arr) {
// You can do this!
var count= []
``````

First, create an empty array called count.

``````  for (var i=0;i<arr.lenght; i++){
``````

First of all, there’s a typo in this line so it won’t work. But if it were fixed, this says the following:
Start a loop. Declare a variable i that starts at 0. While i is less than the length of the array, continue looping. Every time the body of the loop finished running, increase the value of i by one.

``````    var big =0;
``````

Each time the loop runs, create a variable called big and assign it the value 0.

``````    for (var si=0;si < arr[i]; si++){
if (arr[i][si]> big){
big= arr[i][si];
}
``````

Another loop, also with a mistake in the declaration. Do you see the mistake? Once its running correctly, Inside the loop you’re using the values of the two loop variables to look at each value in the arrays and compare them to big, reassigning big if it finds a larger value.

``````    }
count [i] = big
``````

Assign index i of count the value of big. Count is empty, though! We use `arrayName.push(value)` to add something new to the end of an array.

``````  }
return count;
}
``````

Finally, return the completed array.

1 Like

I see the typo i do that all the time i don’t know why very irritating.
then do mean to say my mistake is that big should be less than the arrays.(i plan to chance si to j i think thats confusing me to.)

The mistake is in the loop declaration, not the body’s logic. What does this mean?
`for (var si=0;si < arr[i]; si++)`
… compared to this?
`for (var si=0;si < arr[i].length; si++)`

my gosh i can’t believe I did that

so Its still not working ``````function largestOfFour(arr) {
// You can do this!
var count = []

for (var i = 0; i < arr.lenght; i++) {
var big = 0;
for (var j = 0; j < arr[i].lenght; j++) {
if (arr[i][j] < big) {
big = arr[i][j];
}
count.push(big);
}
}

return count;
}

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

if returning 0 on console

@InternetFriend why do i get
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0

I reset the code done for today  You have a typo here.

You’ve typoed “length” in both loops.

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

This says “If the number we’re looking at is smaller than the biggest number we’ve found, set the biggest number we’ve found to the smaller number.”

Take a look at where you’ve put `count.push(big)`. When do you want to add an item to the result array? When are you adding an item right now?

@RandellDawson, @InternetFriend I’m sorry guys about the typo I may have been doing that my whole life. h then t ham then taters got it.

1 Like