简体   繁体   中英

Compare array element previous and next

This seems super basic, but I'm drawing a blank.

I have a couple for loops, and I want to check the i and j values -1 and +1 but obviously as the array I'm checking doesn't have a arr[0-1] element, it returns an error. How would I fix that?

var islandPerimeter = function(grid) {
let result = 0;

for(var i = 0; i < grid.length; i++) {
  for(var j = 0; j < grid[i].length; j++) {
        if(grid[i][j] === 1) {
            if(grid[i-1][j] !== 1) { //left
                result += 1;
            }
            if(grid[i+1][j] !== 1) { //right
                result += 1;
            }
            if(grid[i][j+1] !== 1) { //bottom
                result += 1;
            }
            if(grid[i][j-1] !== 1) { //top
                result += 1;
            }
        }
      }  
   }
   return result;
};

so where I have the //left comment I get an error:

Uncaught TypeError: Cannot read property '1' of undefined

How can I best prevent this issue? Thank you.

Here is problem description and input and expected output :

You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represents water.

Grid cells are connected horizontally/vertically (not diagonally). The grid is completely surrounded by water, and there is exactly one island (ie, one or more connected land cells).

The island doesn't have "lakes" (water inside that isn't connected to the water around the island). One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.

Input:
[[0,1,0,0],
 [1,1,1,0],
 [0,1,0,0],
 [1,1,0,0]]

Output: 16

You can use the following line as condition

!grid[i-1] || grid[i-1][j] !== 1

This will check if grid[i-1] doesnot exists it will jump in the if statement. If it exists it will check the other condition normal way.

for(var i = 0; i < grid.length; i++) {
  for(var j = 0; j < grid[i].length; j++) {
        if(grid[i][j] === 1) {
            if(!grid[i-1] || grid[i-1][j] !== 1) { //left
                result += 1;
            }
            if(grid[i+1][j] !== 1) { //right
                result += 1;
            }
            if(grid[i][j+1] !== 1) { //bottom
                result += 1;
            }
            if(grid[i][j-1] !== 1) { //top
                result += 1;
            }
        }
      }  
   }
   return result;
}

you can try like this

var islandPerimeter = function(grid) {
let result = 0;

for(var i = 0; i < grid.length; i++) {
  for(var j = 0; j < grid[i].length; j++) {
    if(grid[i][j] === 1) {
    if (i > 0) {    
    if(grid[i-1][j] !== 1) { //left
            result += 1;
        }
     }
        if(grid[i+1][j] !== 1) { //right
            result += 1;
        }
        if(grid[i][j+1] !== 1) { //bottom
            result += 1;
        }
        if(grid[i][j-1] !== 1) { //top
            result += 1;
        }
    }
  }  
 }
  return result;
 };

An initial problem is going to be that on the first pass you're attempting to access grid[i-1] . Which, when i equals 0 , grid[-1] does not exist. With that in mind, some basic validation will ensure only defined cells are checked.

Then you simply need to use the bracket notation to access a property, using the desired value.

var islandPerimeter = function(grid) {
  let result = 0;

  for(var i = 0; i < grid.length; i++) {
    for(var j = 0; j < grid[i].length; j++) {
      if(grid[i][j] === 1) {
        if(grid[i-1] && grid[i-1][j] !== 1) { //left
          result += 1;
        }

        if(grid[i+1] && grid[i+1][j] !== 1) { //right
          result += 1;
        }

        if(grid[i][j+1] && grid[i][j+1] !== 1) { //bottom
          result += 1;
        }

        if(grid[i][j-1] && grid[i][j-1] !== 1) { //top
          result += 1;
        }
      }
    }
  }
  return result;
};

var grid = [
  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
];

console.log(islandPerimeter(grid)); // returns 5 ¯\_(ツ)_/¯

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM