JavaScript: Sum of all odds in a matrix
JavaScript Math: Exercise-91 with Solution
Sum of Odd Elements in Matrix
Write a JavaScript program to calculate the sum of all odd elements in a square matrix.
Test Data:
([ [1, 2, 3], [4, 5, 6], [7, 8, 9] ]) -> 25
( [ [-1, -2], [-4, -5] ]) -> -6
Sample Solution:
JavaScript Code:
Output:
Sum of all odds of the said matrix: 25 Sum of all odds of the said matrix: -6
Flowchart:

Live Demo:
For more Practice: Solve these Related Problems:
- Write a JavaScript function that iterates through a square matrix and sums all the odd numbers found.
- Write a JavaScript function that uses nested loops to accumulate the sum of odd elements in a two-dimensional array.
- Write a JavaScript function that applies the filter method to each row to extract odd numbers before summing them.
- Write a JavaScript function that handles matrices with both positive and negative odd numbers while calculating the sum.
Improve this sample solution and post your code through Disqus.
Previous: Test if a number is a Harshad Number or not.
Next: Iterated Cube Root.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics