# Help w/ returning largest numbers in an arrays

Tell us what’s happening:

I am so close to getting this thing down, but when it comes to negative numbers (passing the last test), I am so dead. If I set the array numbers to negative numbers just to pass the test, then I know I’m cheating. If I empty the array and push the array values in, the final result is always an empty array (`totalArr[i].push(arr[i][j]);`).

Thanks.

``````
function largestOfFour(arr) {
// You can do this!
var totalArr = [0,0,0,0];
for(var i = 0; i < arr.length; i++){
for(var j = 0; j < arr[i].length; j++){
if(arr[i][j] > totalArr[i]){
totalArr[i]=arr[i][j];
}
}
}

}

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

``````

User Agent is: `Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36`.

Challenge: Return Largest Numbers in Arrays

Why start with an array of zeros? Is there a way for you to pick the initial array based on your inputs?

1 Like

So starting with a blank array `[]` would be the right choice?

Let’s say I tell you I have 4 pieces of paper in my pocket with a number (negative or positive) written on each. If I hand them to you one at a time, how would you figure out which one is the smallest?

Focus on the algorithm steps here and do not worry about the code just yet.

2 Likes

To find them smallest value of the pieces, I’d compare each one to the other(s) as they are handed out to me.

I’ve been reading your explanation over and over for like five minutes, but I’m still totally stuck and am wondering how this relates to the algorithm.

Describe what you mean by “compare”. Walk me through the actual steps for each piece of paper.

Right, so when you have the first piece of paper, what’s your best guess at what’s the biggest value?

1. The first one is given to me.
2. I then receive the second piece of paper.
3. I look at them both and pick the one with the smaller numeric value as the smallest one.
4. The third piece of paper is given to me.
5. I look at that one. If its value is greater than that of the previously selected one, then the previous smallest keeps its title of the smallest. If not, then the newest piece of paper is selected as the smallest one.
6. Finally, the last piece of paper is handed to me. I will compare it with the smallest value I have now. If the piece of paper most recently handed to me has a lesser value than that of the last one, IT will be selected as the smallest one and the algorithm ends. If the paper most recently handed to me has a value greater than that of the last one, then the previous one keeps its title of the lesser of all the others. The algorithm ends.

My best (and only) guess at which has the greatest value would be that first piece of paper.

Exactly, so how can you incorporate that in steps 1-2 above? Once you figure that out, you can write the code to solve the challenge. You have the basic gist of the code already.

Do you really care about which one is the smallest? You are supposed to be finding the largest in each sub array.

Maybe by making the array equal to the smallest values of each and then finding the largest ones?

You told me to find the smallest value of each piece of paper. If you meant to find the largest, then I would go over the same algorithm again and instead of finding the one with the smallest value, try to find the largest.

Let me apologize about something that might have confused you a bit. In my hypothetical example, I should have asked how you would know when you found the largest number.

How I would know I’ve found the largest number is when I’ve iterated through them all (after going through each step I explained).

Since I messed my example up, I will explain how I would figure out which piece of paper has the largest number.

I assume the first piece of paper has the largest number and I remember that number as the “largest”.

I am handed the 2nd piece of paper and if it contains a larger number than the one I have assigned to my memory, then I remember the new number as the “largest”.

I am handed the 3rd piece of paper and if it contains a larger number than the one I have assigned to my memory, then I remember the new number as the “largest”.

I am handed the 4th piece of paper and if it contains a larger number than the one I have assigned to my memory, then I remember the new number as the “largest”.

Now that all the pieces of paper have been reviewed, I just have to remember the number I have stored in my memory as the “largest”.

1 Like

This is the kind of process you should go through for all of the challenges. No code written. I just think through the steps of how to do it manually. In the case of above, “my memory” will just be a variable I created to store the largest number found so far.

1 Like

brother @ConnerOw1115 I solved that same problem by using the Math.max/min methods, I won’t tell you the answer but a little hint: to pass an array to one of these methods do

``````Math.max(...arr)
``````

good luck

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.