Advertisement
PowerCell46

Equal Neighbors JS

Feb 17th, 2023
875
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function equalNeighbours(arrayOfArrays) {
  2.  
  3.     let counter = 0;
  4.  
  5.     for (let index = 0; index < arrayOfArrays.length; index++) {
  6.         let currentArray = arrayOfArrays[index];
  7.         let previousArray = arrayOfArrays[index - 1];
  8.         let nextArray = arrayOfArrays[index + 1];
  9.  
  10.         for (let i = 0; i < currentArray.length; i++) {
  11.             let current_number = (currentArray[i]);
  12.  
  13.             if (index > 0) {
  14.                 if(current_number === (previousArray[i])) {
  15.                     counter++;
  16.                 }
  17.             }
  18.             if (index <= arrayOfArrays.length - 2) {
  19.                 if(current_number === (nextArray[i])) {
  20.                     counter++;
  21.                 }
  22.             }
  23.             if (i > 0) {
  24.                 if (current_number === (currentArray[i - 1])) {
  25.                     counter++;
  26.                 }
  27.             }
  28.             if (i <= currentArray.length - 2) {
  29.                 if (current_number === (currentArray[i + 1])) {
  30.                     counter++;
  31.                 }
  32.             }
  33.         }
  34.     }
  35.  
  36. console.log(counter / 2);
  37.  
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement