DEV Community

DPC
DPC

Posted on

6 4 3 5 5

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

Warp.dev image

The best coding agent. Backed by benchmarks.

Warp outperforms every other coding agent on the market, and gives you full control over which model you use. Get started now for free, or upgrade and unlock 2.5x AI credits on Warp's paid plans.

Download Warp

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

}

Scale globally with MongoDB Atlas. Try free.

Scale globally with MongoDB Atlas. Try free.

MongoDB Atlas is the global, multi-cloud database for modern apps trusted by developers and enterprises to build, scale, and run cutting-edge applications, with automated scaling, built-in security, and 125+ cloud regions.

Learn More

πŸ‘‹ Kindness is contagious

Take a moment to explore this thoughtful article, beloved by the supportive DEV Community. Coders of every background are invited to share and elevate our collective know-how.

A heartfelt "thank you" can brighten someone's dayβ€”leave your appreciation below!

On DEV, sharing knowledge smooths our journey and tightens our community bonds. Enjoyed this? A quick thank you to the author is hugely appreciated.

Okay