Ad
Code
Diff
  • function nextGeneration(grid) {
      return grid.map((row, rowIndex) => {
        return row.map((cell, colIndex) => {
          // Non circular grid
          if(!rowIndex || !colIndex || rowIndex == grid.length - 1 || colIndex == row.length - 1) {
            return 0;
          }
    
          const neighbours = grid[rowIndex   ][colIndex -1] +
                             grid[rowIndex   ][colIndex +1] +
                             grid[rowIndex -1][colIndex -1] +
                             grid[rowIndex -1][colIndex   ] +
                             grid[rowIndex -1][colIndex +1] +
                             grid[rowIndex +1][colIndex -1] +
                             grid[rowIndex +1][colIndex   ] +
                             grid[rowIndex +1][colIndex +1];
    
          if(neighbours == 3) return 1;
          else if(neighbours != 2) return 0;
          return cell;
        });
      });
    }
    • function nextGeneration(grid) {
    • return grid.map((row, rowIndex) => {
    • return row.map((cell, colIndex) => {
    • if (rowIndex !== 0 && colIndex !== 0 && rowIndex < grid.length - 1 && colIndex < row.length - 1) {
    • let neighboursCount = 0;
    • if (grid[rowIndex][colIndex + 1] === 1) neighboursCount++;
    • if (grid[rowIndex][colIndex - 1] === 1) neighboursCount++;
    • if (grid[rowIndex + 1][colIndex] === 1) neighboursCount++;
    • if (grid[rowIndex - 1][colIndex] === 1) neighboursCount++;
    • if (grid[rowIndex + 1][colIndex + 1] === 1) neighboursCount++;
    • if (grid[rowIndex + 1][colIndex - 1] === 1) neighboursCount++;
    • if (grid[rowIndex - 1][colIndex + 1] === 1) neighboursCount++;
    • if (grid[rowIndex - 1][colIndex - 1] === 1) neighboursCount++;
    • if (cell === 1) {
    • if (neighboursCount === 2 || neighboursCount === 3 ) {
    • return 1;
    • }
    • } else {
    • if (neighboursCount === 3 ) {
    • return 1;
    • }
    • }
    • // Non circular grid
    • if(!rowIndex || !colIndex || rowIndex == grid.length - 1 || colIndex == row.length - 1) {
    • return 0;
    • }
    • return 0;
    • const neighbours = grid[rowIndex ][colIndex -1] +
    • grid[rowIndex ][colIndex +1] +
    • grid[rowIndex -1][colIndex -1] +
    • grid[rowIndex -1][colIndex ] +
    • grid[rowIndex -1][colIndex +1] +
    • grid[rowIndex +1][colIndex -1] +
    • grid[rowIndex +1][colIndex ] +
    • grid[rowIndex +1][colIndex +1];
    • if(neighbours == 3) return 1;
    • else if(neighbours != 2) return 0;
    • return cell;
    • });
    • });
    • }
Strings
Data Types
Code
Diff
  • let lastChar = string => string[string.length - 1]
    • var lastChar = string => string[string.length - 1]
    • let lastChar = string => string[string.length - 1]
Strings
Data Types
Code
Diff
  • function lastChar(string) {
      return string[string.length - 1];
    }
    • function lastChar(string) {
    • return string.split('').pop();
    • return string[string.length - 1];
    • }