Ad
Code
Diff
  • function getNeighbors(grid, i){
      var neighborHash = [
        [1, 3, 4], [0, 2, 3, 4, 5], [1, 4, 5],
        [0, 1, 4, 6, 7], [0, 1, 2, 3, 5, 6, 7, 8], [1, 2, 4, 7, 8],
        [3, 4, 7], [3, 4, 5, 6, 8], [4, 5, 7]
      ];
      var result = 0;
      var neighbors = neighborHash[i];
      for(var j = 0; j < neighbors.length; j++){
        if(grid[neighbors[j]] === 1){
          result++;
        }
      }
      return result;
    }
    
    function nextGeneration(grid) {
      if(grid.length === 0){
        return [];
      }
      newGrid = [
        0,0,0,
        0,0,0,
        0,0,0
      ];
      for(var i = 0; i < newGrid.length; i++){
        // gets the number of neighbors that are alive for a given cell from the current generation
        var neighbors = getNeighbors(grid, i);
        // these if statements determine whether a cell is alive or dead in the next generation
        if(neighbors > 3 || neighbors < 2){
          newGrid[i] = 0;
        }
        if(grid[i] === 0 && neighbors === 3){
          newGrid[i] = 1;
        }
        if(grid[i] === 1 && (neighbors === 2 || neighbors === 3)){
          newGrid[i] = 1
        }
      }
      return newGrid;
    }
    • function getNeighbors(grid, i){
    • var neighborHash = [
    • [1, 3, 4], [0, 2, 3, 4, 5], [1, 4, 5],
    • [0, 1, 4, 6, 7], [0, 1, 2, 3, 5, 6, 7, 8], [1, 2, 4, 7, 8],
    • [3, 4, 7], [3, 4, 5, 6, 8], [4, 5, 7]
    • ];
    • var result = 0;
    • var neighbors = neighborHash[i];
    • for(var j = 0; j < neighbors.length; j++){
    • if(grid[neighbors[j]] === 1){
    • result++;
    • }
    • }
    • return result;
    • }
    • function nextGeneration(grid) {
    • // grid that gets returned
    • if(grid.length === 0){
    • return [];
    • }
    • newGrid = [
    • 0,0,0,
    • 0,0,0,
    • 0,0,0
    • ];
    • for(var i = 0; i < newGrid.length; i++){
    • // gets the number of neighbors that are alive for a given cell from the current generation
    • var neighbors = getNeighbors(grid, i);
    • // these if statements determine whether a cell is alive or dead in the next generation
    • if(neighbors > 3 || neighbors < 2){
    • newGrid[i] = 0;
    • }
    • if(grid[i] === 0 && neighbors === 3){
    • newGrid[i] = 1;
    • }
    • if(grid[i] === 1 && (neighbors === 2 || neighbors === 3)){
    • newGrid[i] = 1
    • }
    • }
    • return newGrid;
    • }
Code
Diff
  • function getNeighbors(grid, i){
      var neighborHash = [
        [1, 3, 4], [0, 2, 3, 4, 5], [1, 4, 5],
        [0, 1, 4, 6, 7], [0, 1, 2, 3, 5, 6, 7, 8], [1, 2, 4, 7, 8],
        [3, 4, 7], [3, 4, 5, 6, 8], [4, 5, 7]
      ];
      var result = 0;
      var neighbors = neighborHash[i];
      for(var j = 0; j < neighbors.length; j++){
        if(grid[neighbors[j]] === 1){
          result++;
        }
      }
      return result;
    }
    
    function nextGeneration(grid) {
      // grid that gets returned
      if(grid.length === 0){
        return [];
      }
      newGrid = [
        0,0,0,
        0,0,0,
        0,0,0
      ];
      for(var i = 0; i < newGrid.length; i++){
        // gets the number of neighbors that are alive for a given cell from the current generation
        var neighbors = getNeighbors(grid, i);
        // these if statements determine whether a cell is alive or dead in the next generation
        if(neighbors > 3 || neighbors < 2){
          newGrid[i] = 0;
        }
        if(grid[i] === 0 && neighbors === 3){
          newGrid[i] = 1;
        }
        if(grid[i] === 1 && (neighbors === 2 || neighbors === 3)){
          newGrid[i] = 1
        }
      }
      return newGrid;
    }
    • function getNeighbors(grid, i){
    • var neighborHash = [
    • [1, 3, 4], [0, 2, 3, 4, 5], [1, 4, 5],
    • [0, 1, 4, 6, 7], [0, 1, 2, 3, 5, 6, 7, 8], [1, 2, 4, 7, 8],
    • [3, 4, 7], [3, 4, 5, 6, 8], [4, 5, 7]
    • ];
    • var result = 0;
    • var neighbors = neighborHash[i];
    • for(var j = 0; j < neighbors.length; j++){
    • if(grid[neighbors[j]] === 1){
    • result++;
    • }
    • }
    • return result;
    • }
    • function nextGeneration(grid) {
    • return grid;
    • // grid that gets returned
    • if(grid.length === 0){
    • return [];
    • }
    • newGrid = [
    • 0,0,0,
    • 0,0,0,
    • 0,0,0
    • ];
    • for(var i = 0; i < newGrid.length; i++){
    • // gets the number of neighbors that are alive for a given cell from the current generation
    • var neighbors = getNeighbors(grid, i);
    • // these if statements determine whether a cell is alive or dead in the next generation
    • if(neighbors > 3 || neighbors < 2){
    • newGrid[i] = 0;
    • }
    • if(grid[i] === 0 && neighbors === 3){
    • newGrid[i] = 1;
    • }
    • if(grid[i] === 1 && (neighbors === 2 || neighbors === 3)){
    • newGrid[i] = 1
    • }
    • }
    • return newGrid;
    • }