Return Largest Numbers in Arrays | Basic Scripting

``````function largestOfFour(arr) {
let highestArr = [];
let highestNum = 0;
for(let i = 0; i < arr.length; i++) {
for(let j = 0; j < arr[i].length; j++){
if (arr[i][j] > highestNum) {
highestNum = arr[i][j];
}
highestArr.push(highestNum);

}

}
return highestArr;
}
``````

Thank you

Use the console.log() to debug your code and see what is happening:

``````function largestOfFour(arr) {
let highestArr = [];
let highestNum = 0;
for (let i = 0; i < arr.length; i++) {
for (let j = 0; j < arr[i].length; j++) {
if (arr[i][j] > highestNum) {
highestNum = arr[i][j];
}
highestArr.push(highestNum);
console.log("i", i, "j", j, "curRes", highestArr)
}

}
return highestArr;
}

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

Consider the array with all negative numbers - what does the function says itâ€™s the biggest one?

1 Like

Yeah, I came up with this for logging out:

``````function largestOfFour(arr) {
let highestArr = [];
let highestNum = 0;
for(let i = 0; i < arr.length; i++) {
console.log('starting outer loop', i)
console.log('on start, highestNum =', highestNum)
for(let j = 0; j < arr[i].length; j++){
console.log('starting inner loop', j)
console.log(`is arr[i][j] (\${arr[i][j]}) > highestNum (\${highestNum})`, arr[i][j] > highestNum)
if (arr[i][j] > highestNum) {
highestNum = arr[i][j];
}
highestArr.push(highestNum);

}

}
return highestArr;
}

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

In addition to what is said, I would suggest that you consider if you want only one â€śhighestNumâ€ť. Iâ€™m not saying that you need more than one variable, just that you may not want to use that one state for all the numbers. Is that what you are supposed to be checking? You might also want to think about where you are doing your push.

1 Like

yeah, I miss that test. Thanks

Not knowing where to define or return variable is a recurring issue for me. Is there a way to use stepper for debugging?
Thanks for the help!

Oh, by the way, I had to look at the solution to solve it

Not knowing where to define or return variable is a recurring issue for me.

Yeah, that is a common problem. It just takes practice.

Is there a way to use stepper for debugging?

Absolutely. I just think that log statements are easier for beginners. But if you feel comfortable with setting break points and stepping through your code, then go for it.

2 Likes

How to know where to put log statements? We can use it for return statements, so okay, that is one.

You insert log statements wherever you want to know what is going on.

A big part of being a good developer is being a good debugger. And a lot of being a debugger is being a detective. You have to investigate. Just start logging out information and seeing if it matches what you expect it to be. If you log it out and it matches your expectation, move forward and find where it stops. If it doesnâ€™t match your expectations, more backwards and find where it stopped. Try it with different data to see how it handles different conditions.

3 Likes