Tell us what’s happening:
Nested for (let j =0…) loop doesn’t see ‘maxElem’ and ‘i’ variables. Why?
I tried to replace let with var, but it’s no use(
Where is the mistake?
Your code so far
function largestOfFour(arr) {
// You can do this!
let maxElemArr = [];
for (let i = 0; i < arr.length; i++) {
// not working. Nested for (let j =0...) loop doesn't see maxElem and i variables. Why?
let maxElem = arr[i][0];
for (let j = 1; j < arr[i].lenght; j++){
console.log(i);
if (maxElem < arr[i][j]) maxElem = arr[i][j];
}
maxElemArr.push(maxElem);
//working solusion variant 2
// maxElemArr.push(Math.max(...arr[i]));
}
console.log(maxElemArr);
return maxElemArr;
}
largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3792.0 Safari/537.36
.
Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/return-largest-numbers-in-arrays