Advertisement
elena1234

Find Equal Pairs (work with matrix) - JavaScript

Aug 18th, 2021
1,313
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function calculateEqualPairs(matrix) {
  2.     let pairs = 0;
  3.     for (let row = 0; row <= matrix.length - 1; row++) {
  4.         for (let col = 0; col <= matrix[row].length - 1; col++) {
  5.             if (row + 1 <= matrix.length - 1)
  6.                 if (matrix[row][col] === matrix[row + 1][col]) {
  7.                     pairs++;
  8.                 }
  9.  
  10.             if (col + 1 <= matrix[row].length - 1)
  11.                 if (matrix[row][col] === matrix[row][col + 1]) {
  12.                     pairs++;
  13.                 }
  14.         }
  15.     }
  16.     console.log(pairs);
  17. }
  18.    
  19. calculateEqualPairs([['2', '3', '4', '7', '0'],
  20. ['4', '0', '5', '3', '4'],
  21. ['2', '3', '5', '4', '2'],
  22. ['9', '8', '7', '5', '4']]
  23. )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement