Call each level in a three dimensional array?

Hi all,
LC101 independent -> Week 2A Exercises -> ArrayExercises06 -> section e

I am needing help with figuring out why I am not calling all three levels of my 3 dimensional array. Can someone point me in the right direction? Here’s what I have for the section but looking at my repl.it might be more useful.

//e) ‘table’ is an example of a 2-dimensional array. The first “level” contains the element arrays, and the second level holds the name/symbol/mass values. Experiment! Create a 3-dimensional array and print out one entry from each level in the array.

let classroom = [‘chemistry’];

table.push(element1, element2, element3);

classroom.push(table);

console.log(classroom);

console.log(classroom[0][1]);

console.log(classroom[1][1][0]);

console.log(classroom[2][0]);

https://repl.it/@ChristinaVarghe/ArrayExercises06#index.js

p.s. not sure why someone marked flagged this? Please reach out to me and let me know if I did something wrong.

Hi Christine. I just looked at your code and ran it. Your error code is a runtime error telling you that 0 references an undefined array.
Right now, the array classroom contains two sub-arrays (‘chemistry’ and ‘table’.). These have the index values of 0 and 1, respectively. Trying to reference anything from index value 2 is impossible. If you push another array into classroom (say ‘physics’, for example) you will be able to print classroom[2]. But you will still get an error message if you key in console.log(classroom[2][0]) because there would only be one “level” below classroom at index 2.
If you try to print anything from the ‘chemistry’ array (say console.log(classroom[0][1]) you should get an error message, too, since ‘chemistry’ has no sub-arrays.

I hope this helps.

1 Like

Look at it this way. We have a bookshelf with 3 shelves that we need to put things on. Let’s call this bookshelf ‘classroom.’
The top shelf we are going to label with a sticker that says 0, and we are going to call that specific shelf ‘table’. On that bookshelf are 3 books, one titled hydrogen, one titled helium, and one titled iron. Each book has two additional properties that appear on their covers, their mass and their symbol. In position 0, we have hydrogen, in position 1 we have helium, and in position 2 we have iron.
If I wanted to find helium, I would look for it on shelf 0, in position 1. If I wanted to be even more specific, I could look for the mass in a specific position on the cover (in this case, in position 2 on the cover of the book–that is, if you’re still following my metaphor.)
Let’s say you get 3 more books, to go on the shelf you are going to call… I don’t know, maybe grid. The shelf, whatever you are going to call it, will be in position 1. (This is a new array). Pick 3 more books, each with 3 properties that would appear on the cover in specific places. The first book will be in position 0, the second in position 1, and the third in position 2. Whatever specific characteristics you are including are on the covers in positions 0, 1, and 2 as well.

Just like you pushed the arrays element1, element2, and element26 into the array table, you will push new arrays into this new array, and then you will push that array into the larger framework (or onto the bookshelf, as it were).

I know this is not a very code-specific answer, but I think once you get your head around the idea it is pretty easy to code.

LMK if this helps you to get the concept.

1 Like

Thank you so much. I love your metaphor!!! I think I am understanding it better! It won’t let me post a link but the repl.it is the same.