0

Can someone please explain what this error means!

TypeError: Cannot read properties of undefined (reading 'length') From this code: function largestOfFour(arr) { var maxx = [0,0,0,0]; for(var i = 0; i < arr.length; i++) { for(var j = 0; j < arr[i].length; j++) { if(arr[i][j] > maxx[i]) { maxx[i] = arr[i][j]; } } } return maxx; } largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);

27th Jan 2022, 12:40 PM
Ntethelelo Shabalala
Ntethelelo Shabalala - avatar
2 Answers
+ 2
I would recommend you to give One default value to 'arr' If user forget to give value to "largestOfFour" while calling , it will through an error "TypeError: Cannot read properties of undefined (reading 'length') " Where undefined means "arr" since user forgot to give value so arr = undefined How to overcome with this ? - By given default value to "arr" Method 1 : arr ||= [] Method 2 : function largestOfFour(arr = [] ) { //Your code }
27th Jan 2022, 1:03 PM
Pariket Thakur
Pariket Thakur - avatar
0
It's working without errors for me.. Do you changed anything? You are not printing returned value so it don't show any output.. Save code and share link..
27th Jan 2022, 1:00 PM
Jayakrishna šŸ‡®šŸ‡³