• in
InterCourses
CoursesBlogs
0
← Introduction to JavaScript
○What is JavaScript?○Your First JavaScript Code○Data Types○Type Checking
○Declaring Variables○let and const in Practice○Operators○Type Conversion
○Conditionals○Switch Statement○For Loops○While Loop○Break and Continue
○Defining Functions○Arrow Functions○Scope○Closures○Project - Calculator○Function Expressions
○Arrays○Array Mutation Methods○Array Search and Slice○Array Iteration Methods●Multidimensional Arrays○Destructuring○Array Reverse and Sort○Array Fill○Array Find and Includes○Array Flat and FlatMap○Array Reduce, Some, and Every
○Objects○Object Methods and Destructuring○JSON○Math and Date○Date Basics
○02 Template Literals○String Methods○Regular Expressions
○Higher-Order Functions○Callbacks○map, filter, and reduce○Recursion○Project - Data Transform Pipeline
○OOP Introduction○Classes○Inheritance○Encapsulation
○Asynchronous JavaScript○Promises and Async Await○Async API Simulation

Multidimensional Arrays

A multidimensional array is an array of arrays. The most common use is a 2D array (matrix) to represent tabular data.

Creating a 2D array

javascript
const matrix = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
];

Accessing elements

javascript
matrix[0][0]; // 1 — row 0, col 0
matrix[1][2]; // 6 — row 1, col 2
matrix[2][1]; // 8 — row 2, col 1

Iterating with nested loops

javascript
for (let row = 0; row < matrix.length; row++) {
    for (let col = 0; col < matrix[row].length; col++) {
        console.log(matrix[row][col]);
    }
}

Your Task

Given this 3×3 matrix:

javascript
const grid = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
];
  1. Write a function matrixSum(matrix) that returns the sum of all elements.
  2. Write a function diagonal(matrix) that returns an array of the main diagonal elements (top-left to bottom-right).
  3. Display in #output: "Sum: 45 | Diagonal: 1,5,9".
Hint 1
1 / 3
HINT 1

Access elements with two indices: matrix[row][col].

Loading editor…
READY
intercourses
javascript