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
EasyCreate 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 functionThis lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Logic & Flow
1Strings In Depth
String FundamentalsIterate Over StringsTemplate LiteralsString MethodsRecap - String Weaver4JSON Part 2
Iterate Over JSONNested JSONJSON Optional ChainingShallow And Deep CopyRecap - Bicycle ShopRecap - Solar System10Manage Festival System
Project OverviewAdd Movies & Venues2Multi-dimensional Arrays
2D Arrays BasicsAccessing 2D Array ElementsNested Loops with 2D ArraysRecap - 2D ArraysMatrix Addition & SubstractionJagged Arrays3D Arrays And BeyondCommon 2D Array PatternsRecap - All About Arrays5Sets Part 1
What Is A Set?Iterating Over SetsAdding An ElementRemoving An ElementChecking If An Element ExistsSize And Is EmptyCopy And ClearRecap - Basic Of Sets8Arrays Interesting Topics
Array DestructuringSpread Syntax in ArraysSparse ArraysRecap - Arrays Workshop3JSON Part 1
What is a JSON?Check If Key ExistsObject MethodsThe Spread Operator Part 1The Spread Operator Part 2Remove KeysRecap - JSON Manipulate Keys