JS Startup

Challenges, Tips & Tricks & Tutorials

JavaScript Quiz – solve 2d array matrix diagonal addition

JavaScript Quiz – solve 2d array matrix diagonal addition

Array Matric Diagonal

In javascript, an array can be 2d array matrix or 2d dimensional which means array contains an array of equal length. Like 3 X 3, the main array length is 3, and each nested array also of the same length and it becomes diagonal.

2D Array Matrix Example

[ [1, 2, 3],
   [4, 5, 6],
   [7, 8, 9] ]

JavaScript Question

In this javascript quiz, find out to solve 2d array matrix diagonal addition difference with the array reduce method.

First, we declare an 2d matrix dimensional array of 3X3 array and assign to variable array using let.

let array = [ [1, 2, 3],
                      [4, 5, 6],
                      [7, 8, 9] ];

Now, create an another variable len using and assign an array length to it.

let len = array.length;

In the next step, create another variable total using let and assign the result return from array reduce method.

let total = array.reduce((add, el, i, arr) => {
  return add + el[i] + el[len-i-1];
},0);

For more detail about array reduce method click here.

Now, finally we test the value of total using console log.

console.log(total);

Any idea, what should be the output above statement.

JavaScript Quiz Test

Here is the full code snippet for you to solve.

let array = [ [1, 2, 3],
                      [4, 5, 6],
                      [7, 8, 9] ];

let len = array.length;

let total = array.reduce((add, el, i, arr) => {
  return add + el[i] + el[len-i-1];
},0);

console.log(total); // output => ?

Answer – 30

Click To Run Code

Explanation

Now, you know the answer is “30”. But why, what actually happens does it give a total of all element. Let me explain.

So, we add diagonal values from left to right and right to left. Let see all array matrix diagonal values

1, , ,
 ,5, ,
 , ,9

// gives 15

And the next diagonal matrix value.

 , ,3
 ,5, 
7, , ,

// gives

And when we add both the values, which we are doing in reduce method.

At last, That’s why we get an answer “30.

Let me know in the comment section, what you think about it.

I hope you understand the concept and logic behind it.

But wait, i have something more for you –

Check out our other javascript quiz – 

  1. JavaScript Quiz – delete work on object create method object
  2. JavaScript Quiz – null and undefined act as value in function parameter
  3. JS Quiz – Math random in if condition
  4. JS Quiz – three dots with string variable will expand

Conclusion

To participate in our javascript quiz or challenges, tutorial, tips & tricks make sure to join our jsstartup newsletter. So, you can able to participate in our daily challenges & learn the javascript concept.

And last but not the least, don’t forget to like, comment and share. It gives us a morale boost to remain to continue. Also, join our Facebook Page

If you have any questions, please feel free to ask me in the comment section and also let me know if you have any suggestions. As suggestions are always welcome.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top
0 Shares
Share via
Copy link