# Basic Algorithm Exercise: Code Works But only half

Tell us what’s happening:
Describe your issue in detail here.

Hi everyone. I’m currently doing Basic Algorithm Scripting: Return Largest Numbers in Arrays
As you can see the code works for 2 out of 4 and I can’t figure out the problem for the life of me.

It’s supposed to assign the biggest value when comparing the maxArr to individual number. It manages to do it for the 2 out of 4 but when it fails it just copies the previous biggest value.

I also attached the picture for more clarity:

``````  **Your code so far**
``````
``````
function largestOfFour(arr) {
let maxArr;
let result = []
//////Creating a sub array
for (let i = 0; i < arr.length; i++){
let subArr = arr[i];
/////Separating a sub array into single numbers
for (let j = 0; j < subArr.length; j++){
let arrNum = subArr[j];
if (maxArr == undefined){  /// if maxArr undefined assign arrNum value
maxArr = arrNum;
}else if ////and once it finds the biggest value assign the new value to maxArr
(arrNum > maxArr){
maxArr = arrNum;
}
}
result.push(maxArr)  /// push the maxArr value to the result variable
}
console.log(result)
return result; ///return that value
}

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

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

largestOfFour([[4, 9, 1, 3], [13, 35, 18, 26], [32, 35, 97, 39], [1000000, 1001, 857, 1]])

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/91.0.4472.77 Safari/537.36`

Challenge: Return Largest Numbers in Arrays

Link to the challenge:

Look at the output of the function

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

Now, look at the numbers in the array. Can you tell why 27 would repeat up until 39?

Here is the other call:

`console.log(largestOfFour([[17, 23, 25, 12], [25, 7, 34, 48], [4, -10, 18, 21], [-72, -3, -17, -10]])) // [ 25, 48, 48, 48 ]`

You need to reset `maxArr` after you push it. You can use `-Infinity`.

Infinity - JavaScript | MDN

Alternatively, move `maxArr` inside the outer loop so that it’s `undefined` at the start of each iteration.

2 Likes

That works as well and I guess with the current logic makes more sense. For some reason, I was also thinking the use of a large negative number was safer for some cases, but I guess not.

Anyway, it’s way too late for me to be looking at code right now so I probably shouldn’t.

Edit: Note the output of the numbers in general – Imo it is the main problem where the variable maxArr survives even after the first for loop is done, and before the second.

Summary

If it does not, that error will not exist at the first place. This means you only “let maxArr = undefined/null/etc” inside the for loop. When it exits the first for cycle, it dissapears. But of course, that’s just my own method to ensure whatever useless now, I throw it away. There are more than one way to do things.

Here’s what I mean:

Summary
``````/* your code still but I moved something */
function largestOfFour(arr) {
let result = [];
let subArr;
for (let i = 0; i < arr.length; i++){
subArr = arr[i];
let maxArr;
for (let j = 0; j < subArr.length; j++){
let arrNum = subArr[j];
if (maxArr == undefined){
maxArr = arrNum;
} else if (arrNum > maxArr){
maxArr = arrNum;
}

}
result.push(maxArr);
}
console.log(result)

return result; ///return that value
}
largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);

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

largestOfFour([[4, 9, 1, 3], [13, 35, 18, 26], [32, 35, 97, 39], [1000000, 1001, 857, 1]])
``````

Thanks guys.
I have moved the variable maxArr inside the first for loop and it works fine.

Still unsure why it doesn’t work the way I wrote it but ok.

You need to ‘reset’ your maximum value between subarrays. Otherwise you’ll be finding the best maximum of all array values seen thus far.

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