Error : undefined

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

I am getting an error that says “Cannot set properties of undefined”, although I cannot see what is undefined here.
Your code so far


function chunkArrayInGroups(arr, size) {
let myArr = [];
let counter = arr.length / size;
let base = 0;
for(let i = 0; i < counter; ++i){
  let subArray = 0;
  for(let a = 0; a < size; ++a) {
myArr[i][subArray] = arr[base];
++subArray;
  }
  base = base + size;
   }
   console.log(myArr);
return myArr;
}

chunkArrayInGroups([0, 1, 2, 3, 4, 5], 3);
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36

Challenge: Chunky Monkey

Link to the challenge:

When in doubt, add more console.log()s:

function chunkArrayInGroups(arr, size) {
  let myArr = [];
  let counter = arr.length / size;
  let base = 0;
  for (let i = 0; i < counter; i++) {
    let subArray = 0;
    console.log("iteration : " + i);
    console.log("  subArray : ", subArray);
    for (let a = 0; a < size; a++) {
      console.log("  inner iteration : " + a);
      console.log("    myArr : ", myArr);
      myArr[i][subArray] = arr[base];
      subArray++;
    }
    base = base + size;
  }
  console.log(myArr);
  return myArr;
}

chunkArrayInGroups([0, 1, 2, 3, 4, 5], 3);

I am sorry but I still couldn’t figure it out even after adding the console.logs. The undefined warning makes me think that I am trying to manipulate an element of an array that’s not defined but am I not supposed to declare an empty array? Why would it be defined anyways?

function chunkArrayInGroups(arr, size) {
  let myArr = [];
  let counter = arr.length / size;
  let base = 0;
  for(let i = 0; i < counter; ++i){
    let subArray = 0;
    
    for(let a = 0; a < size; ++a) {
      
myArr[i][subArray] = arr[base];
++subArray;
++base;

    }

     }
     console.log(myArr);
  return myArr;
}

chunkArrayInGroups([0, 1, 2, 3, 4, 5], 3);

When I run this:

function chunkArrayInGroups(arr, size) {
  let myArr = [];
  let counter = arr.length / size;
  let base = 0;
  for (let i = 0; i < counter; i++) {
    let subArray = 0;
    console.log("iteration : " + i);
    console.log("  subArray : ", subArray);
    for (let a = 0; a < size; a++) {
      console.log("  inner iteration : " + a);
      console.log("    myArr : ", myArr);
      myArr[i][subArray] = arr[base];
      subArray++;
    }
    base = base + size;
  }
  console.log(myArr);
  return myArr;
}

chunkArrayInGroups([0, 1, 2, 3, 4, 5], 3);

I get:

iteration : 0
  subArray :  0
  inner iteration : 0
    myArr :  []
TypeError: myArr[i] is undefined

So, myArr[i] is not defined. But, if myArr[i] is not defined, then it is impossible for myArr[i][anything] to exist. You can’t assign something to a property/index of a thing that doesn’t exist.

1 Like

Actually, I have tried removing the [subArray] and I don’t see that warning anymore, so it is probably about that part.

So I can’t add values to an empty array without using .push?

You can add values to an empty array. But that’s not what you are doing. You are trying to add values to a subarray of an empty array.

// This is ok
const myArray = [];
myArray[2] = 5;
console.log(myArray);

// This is bad
const mySecondArray = [];
mySecondArray[0][2] = 5;
console.log(mySecondArray);
1 Like

Okay, I see what I was doing wrong and I was able to pass the challenge using .push(), but I am curious if there is a way to create subarrays of an empty array without using .push function.

Sure, you just need to define mySecondArray[0] before trying to use it:

// This is ok
const myArray = [];
myArray[2] = 5;
console.log(myArray);

// This is ok but wierd
const mySecondArray = [];
mySecondArray[0] = [];
mySecondArray[0][2] = 5;
console.log(mySecondArray);

// This is bad
const myThirdArray = [];
myThirdArray[0][2] = 5;
console.log(myThirdArray);
1 Like