Menu
Coddy logo textTech

3D Arrays And Beyond

Part of the Logic & Flow section of Coddy's JavaScript journey — lesson 12 of 65.

A 3D array can be visualized as an array of 2D arrays. For example, think of multiple buildings, each with multiple floors, each floor having multiple rooms. Access an element using three indices: the first for the building, the second for the floor, and the third for the room.

For instance:

const buildings = [
  [
    ["R1", "R2"],
    ["R3", "R4"]
  ],
  [
    ["R5", "R6"],
    ["R7", "R8"]
  ]
];
// buildings[0][1][1] -> "R4"
// buildings[1][0][0] -> "R5"

This structure extends the 2D concept one level deeper, making it practical for scenarios with multiple layers of data.

challenge icon

Challenge

Easy

Create a function named countAllStrings that receives a 3D array of strings and returns the total number of elements across all dimensions.

Cheat sheet

A 3D array is an array of 2D arrays. Access elements using three indices: [building][floor][room].

const buildings = [
  [
    ["R1", "R2"],
    ["R3", "R4"]
  ],
  [
    ["R5", "R6"],
    ["R7", "R8"]
  ]
];

// Access elements
buildings[0][1][1] // "R4"
buildings[1][0][0] // "R5"

Try it yourself

function countAllStrings(arr3D) {
  // TODO: Calculate the total number of string elements in the 3D array
}
// Do not write anything outside function
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Logic & Flow