# Need some guidance

Tell us what’s happening:

``````  Describe your issue in detail here :
``````

This is my code for returning the largest numbers in the arrays. The problem is that I have set the initial value to zero.
So the negative numbers are not considered and instead the value shown is 0. Hence instead of the greatest negative number it shows 0. Atleast that’s what I think.
How do I salvage the situation?

``````  **Your code so far**
``````
``````
function largestOfFour(arr) {
let largest=[0,0,0,0];
//here i is array index of the array and j is the sub-array index.
for (let i =0;i<arr.length;i++) {
for (let j =0;j<arr[i].length;j++)
if(arr[i][j] > largest[i]){
largest[i] = arr[i][j];
}
}
return largest;
}

largestOfFour([[17, 23, 25, 12], [25, 7, 34, 48], [4, -10, 18, 21], [-72, -3, -17, -10]]);
``````
``````  **Your browser information:**
``````

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

Challenge: Return Largest Numbers in Arrays

Link to the challenge:

well, what else you could use as starting value? there are two distinct possibilities that you could use, both would work. You just need to find one of them, and it will work

1 Like

Please elaborate. What should I do? I am a slow learner.

Try to figure out what you can use as starting value instead of 0 - as 0 is always bigger than negative values, you can’t use it if the array contains only negative values, so you need to figure out an alternative

Any suggestions plz.

problem solving is a skill you also need to develop

so I will give a small hint for both possible directions, try to make it work with both

hint 1 - you could use as starting value a number you already have there

hint 2 - you could use as starting value the smallest possible number

This really helped think in the new direction . Thanks .

Thank you for your guidance . I finally did it.
Here’s the code:

``````function largestOfFour(arr) {
let storage=[];
for(let arrayIndex =0 ; arrayIndex<arr.length;arrayIndex++) {
let largestNumber=arr[arrayIndex][0];
for(let subArrayIndex=1;subArrayIndex<arr[arrayIndex].length;subArrayIndex++){
if(arr[arrayIndex][subArrayIndex]>largestNumber){
largestNumber=arr[arrayIndex][subArrayIndex];
}
}
storage[arrayIndex]=largestNumber;
}
return storage;
}
``````

Hi @ameenarab5 !

Congrats on solving the problem.
I edited your post to include spoiler tags since this is a full working solution.

Thanks ! I don’t use forum much . So I didn’t knew how to do that.

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