Skip to main content
added 408 characters in body
Source Link
RamaM
  • 71
  • 1
  • 6

Write a function that takes 2 inputs:

  • a matrix, for example 2-dimensional array, and
  • indices, for example [row, col]

This function should return the sum of all the second input's neighbors (up, down, left, right, diagonals).

Below is my solution. Just wanted to know if there is a better way to solve this problem. And also wanted to know if I am covering all the edge cases.

function findingNeighborsfindingNeibors(myArray, i, j) {
     
let rowMaxrowLimit = myArray.length-1;
  let colMax =columnLimit= myArray[0].length-1;
  let sum = 0;

if(i<0 || j< 0) {
    console.log("invalid Index")
    return
  };
  
if(i>rowLimit || j> columnLimit){
    console.log("You are Out Of Bound");
    return;
}

  for(let x=Mathx = Math.max(0,i-1); x<=Math.min(i+1, rowMaxrowLimit); x++){
    for(let y=Mathy = Math.max(0,j-1); y<=Math.min(j+1, colMaxcolumnLimit); y++){
      if(x!==i || y!==j){
        sum+=myArray[x][y];console.log(myArray[x][y]);
        sum+=myArray[x][y];
    }
    }
  }
  return sum;
}


let input = [
  [0,1,2,3,4],
  [1,2,3,4,5],
  [2,3,4,5,6]
]


findingNeibors(input,0,0);
findingNeibors(input,2,2,0);
findingNeibors(input,-1,3);
findingNeibors(input,3,4);

```

Write a function that takes 2 inputs:

  • a matrix, for example 2-dimensional array, and
  • indices, for example [row, col]

This function should return the sum of all the second input's neighbors (up, down, left, right, diagonals).

Below is my solution. Just wanted to know if there is a better way to solve this problem. And also wanted to know if I am covering all the edge cases.

function findingNeighbors(myArray, i, j){
  let rowMax = myArray.length-1;
  let colMax = myArray[0].length-1;
  let sum = 0;
  for(let x=Math.max(0,i-1); x<=Math.min(i+1, rowMax); x++){
    for(let y=Math.max(0,j-1); y<=Math.min(j+1, colMax); y++){
      if(x!==i || y!==j){
        sum+=myArray[x][y];
      }
    }
}
  return sum;
}

Write a function that takes 2 inputs:

  • a matrix, for example 2-dimensional array, and
  • indices, for example [row, col]

This function should return the sum of all the second input's neighbors (up, down, left, right, diagonals).

Below is my solution. Just wanted to know if there is a better way to solve this problem. And also wanted to know if I am covering all the edge cases.

function findingNeibors(myArray, i,j) {
     
let rowLimit = myArray.length-1;
let columnLimit= myArray[0].length-1;
let sum = 0;

if(i<0 || j< 0) {
    console.log("invalid Index")
    return
  };
  
if(i>rowLimit || j> columnLimit){
    console.log("You are Out Of Bound");
    return;
}

  for(let x = Math.max(0,i-1); x<=Math.min(i+1,rowLimit); x++){
  for(let y = Math.max(0,j-1); y<=Math.min(j+1,columnLimit); y++){
    if(x!==i || y!==j){
        console.log(myArray[x][y]);
        sum+=myArray[x][y];
    }
    }
  }
  return sum;
}


let input = [
  [0,1,2,3,4],
  [1,2,3,4,5],
  [2,3,4,5,6]
]


findingNeibors(input,0,0);
findingNeibors(input,2,2,0);
findingNeibors(input,-1,3);
findingNeibors(input,3,4);

```
punctuation and formatting
Source Link
Oh My Goodness
  • 4.2k
  • 1
  • 11
  • 26

Find sum of neighbors 2din a 2D array

Write a function where the functionthat takes in 2 inputs:

  • a matrix, for example 2-dimensional array, and
  • and indices:, for example [row, col]

This function should return the sum of all the second inputsinput's neighbors (up, down, left, right, diagonals).

Below is my solution. Just wanted to know if there is a better way to solve this problem. And also wanted to know if I am covering all the edge cases.

function findingNeighbors(myArray, i, j){
  let rowMax = myArray.length-1;
  let colMax = myArray[0].length-1;
  let sum = 0;
  for(let x=Math.max(0,i-1); x<=Math.min(i+1, rowMax); x++){
    for(let y=Math.max(0,j-1); y<=Math.min(j+1, colMax); y++){
      if(x!==i || y!==j){
        sum+=myArray[x][y];
      }
    }
}
  return sum;
}```

Find sum of neighbors 2d array

Write a function where the function takes in 2 inputs:

  • a matrix, for example 2-dimensional array
  • and indices: for example [row, col]

This function should return the sum of all the second inputs neighbors (up, down, left, right, diagonals).

Below is my solution. Just wanted to know if there is a better way to solve this problem. And also wanted to know if I am covering all the edge cases.

function findingNeighbors(myArray, i, j){
  let rowMax = myArray.length-1;
  let colMax = myArray[0].length-1;
  let sum = 0;
  for(let x=Math.max(0,i-1); x<=Math.min(i+1, rowMax); x++){
    for(let y=Math.max(0,j-1); y<=Math.min(j+1, colMax); y++){
      if(x!==i || y!==j){
        sum+=myArray[x][y];
      }
    }
}
  return sum;
}```

Find sum of neighbors in a 2D array

Write a function that takes 2 inputs:

  • a matrix, for example 2-dimensional array, and
  • indices, for example [row, col]

This function should return the sum of all the second input's neighbors (up, down, left, right, diagonals).

Below is my solution. Just wanted to know if there is a better way to solve this problem. And also wanted to know if I am covering all the edge cases.

function findingNeighbors(myArray, i, j){
  let rowMax = myArray.length-1;
  let colMax = myArray[0].length-1;
  let sum = 0;
  for(let x=Math.max(0,i-1); x<=Math.min(i+1, rowMax); x++){
    for(let y=Math.max(0,j-1); y<=Math.min(j+1, colMax); y++){
      if(x!==i || y!==j){
        sum+=myArray[x][y];
      }
    }
}
  return sum;
}
readability
Source Link
dfhwze
  • 13.9k
  • 3
  • 38
  • 101

Write a function where the function takes in 2 inputs a matrix, for example 2-dimensional array, and indices: for example [row, col]

  • a matrix, for example 2-dimensional array
  • and indices: for example [row, col]

This function should return the sum of all the second inputs neighbors (up, down, left, right, diagonals).

Below is my solution. Just wanted to know if there is a better way to solve this problem.And And also wanted to know if I am covering all the edge cases.

function findingNeighbors(myArray, i, j){
  let rowMax = myArray.length-1;
  let colMax = myArray[0].length-1;
  let sum = 0;
  for(let x=Math.max(0,i-1); x<=Math.min(i+1, rowMax); x++){
    for(let y=Math.max(0,j-1); y<=Math.min(j+1, colMax); y++){
      if(x!==i || y!==j){
        sum+=myArray[x][y];
      }
    }
}
  return sum;
}```

Write a function where the function takes in 2 inputs a matrix, for example 2-dimensional array, and indices: for example [row, col] This function should return the sum of all the second inputs neighbors (up, down, left, right, diagonals)

Below is my solution. Just wanted to know if there is a better way to solve this problem.And also wanted to know if I am covering all the edge cases.

function findingNeighbors(myArray, i, j){
  let rowMax = myArray.length-1;
  let colMax = myArray[0].length-1;
  let sum = 0;
  for(let x=Math.max(0,i-1); x<=Math.min(i+1, rowMax); x++){
    for(let y=Math.max(0,j-1); y<=Math.min(j+1, colMax); y++){
      if(x!==i || y!==j){
        sum+=myArray[x][y];
      }
    }
}
  return sum;
}```

Write a function where the function takes in 2 inputs:

  • a matrix, for example 2-dimensional array
  • and indices: for example [row, col]

This function should return the sum of all the second inputs neighbors (up, down, left, right, diagonals).

Below is my solution. Just wanted to know if there is a better way to solve this problem. And also wanted to know if I am covering all the edge cases.

function findingNeighbors(myArray, i, j){
  let rowMax = myArray.length-1;
  let colMax = myArray[0].length-1;
  let sum = 0;
  for(let x=Math.max(0,i-1); x<=Math.min(i+1, rowMax); x++){
    for(let y=Math.max(0,j-1); y<=Math.min(j+1, colMax); y++){
      if(x!==i || y!==j){
        sum+=myArray[x][y];
      }
    }
}
  return sum;
}```
Source Link
RamaM
  • 71
  • 1
  • 6
Loading