Return Largest Numbers in Arrays What's that undefined doing here?

Tell us what’s happening:

Hello, I don’t want answers to the challenge. I’m having problems with the data entering the variable “a” in the for, when adding data to the array “a” I don’t understand why a string is entered, nor do I understand why there is an undefined before each block of arrays that is added. I tried the code in the pythontutor page, but it didn’t help me much.

I appreciate very much the help, sorry for the inconvenience.

Your code so far


function largestOfFour(arr) {
// You can do this!
let a = [];
let b = 0;

for (let i = 0; i < arr.length; i++) {
  a[i] += arr[i];
   
}
  console.log(a)

return arr;
}

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; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.70 Safari/537.36.

Challenge: Return Largest Numbers in Arrays

Link to the challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-algorithm-scripting/return-largest-numbers-in-arrays

HINT: Remember that arr is a two-dimensional array. This means it is an array of arrays. When you reference arr[i] you are referencing one of the subarrays of arr. Also, since a starts as an empty array, a[i] will not have a value, so it would be undefined. You are using the += operator which will concatenate the value of what is on the left side of it with the value of what is on the right side of it. The undefined comes from the left side.

2 Likes

It was all the fault of this operator “+=”, hahaha. Thanks for your help, I will continue to solve the challenge.