DEV Community

DPC
DPC

Posted on

5 4 5 3 4

Daily JavaScript Challenge #JS-141: Add Numbers in a Matrix Diagonal

Daily JavaScript Challenge: Add Numbers in a Matrix Diagonal

Hey fellow developers! πŸ‘‹ Welcome to today's JavaScript coding challenge. Let's keep those programming skills sharp!

The Challenge

Difficulty: Easy

Topic: Matrix Manipulation

Description

Create a function that takes a square matrix (2D array) as input and returns the sum of the numbers in the matrix's main diagonal. The main diagonal of a matrix consists of the elements starting from the top left corner to the bottom right corner.

Ready to Begin?

https://www.dpcdev.com/

  1. Fork this challenge
  2. Write your solution
  3. Test it against the provided test cases
  4. Share your approach in the comments below!

Want to Learn More?

Check out the documentation about this topic here: https://en.wikipedia.org/wiki/Matrix_(mathematics)

Join the Discussion!

  • How did you approach this problem?
  • Did you find any interesting edge cases?
  • What was your biggest learning from this challenge?

Let's learn together! Drop your thoughts and questions in the comments below. πŸ‘‡


This is part of our Daily JavaScript Challenge series. Follow me for daily programming challenges and let's grow together! πŸš€

javascript #programming #coding #dailycodingchallenge #webdev

Tiugo image

Modular, Fast, and Built for Developers

CKEditor 5 gives you full control over your editing experience. A modular architecture means you get high performance, fewer re-renders and a setup that scales with your needs.

Start now

Top comments (1)

Collapse
 
asfiaaiman profile image
Asfia Aiman β€’

function twoDimensionalArray(arrayOne) {
let sum = 0;

for (let i = 0; i < arrayOne.length; i++) {
    if (!Array.isArray(arrayOne[i]) || arrayOne[i][i] === undefined) {
        throw new Error("Invalid input: Must be a square 2D array.");
    }
    sum += arrayOne[i][i];
}

return sum;
Enter fullscreen mode Exit fullscreen mode

}

Retry later
πŸ‘‹ Kindness is contagious

Dive into this thoughtful article, cherished within the supportive DEV Community. Coders of every background are encouraged to share and grow our collective expertise.

A genuine "thank you" can brighten someone’s dayβ€”drop your appreciation in the comments below!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found value here? A quick thank you to the author makes a big difference.

Okay