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: