Skip to content Skip to sidebar Skip to footer

How To Generate A Random Starting Position For The Game Of Life?

ERROR: SyntaxError: pathToFiles/Board.jsx: Unexpected token, expected , (32:20) while parsing file: pathToFiles/Board.jsx CODE: Board.jsx generateRandomBoard() { for (var

Solution 1:

It looks like you have a syntax error in the function generateRandomBoard() of the Board component:

generateRandomBoard() {
  for (var i = 0; i < this.state.cells.length ; i++) {
    var currentAlive;
    if(Math.random() < 0.5) {
      currentAlive = false;
    }
    else {
       currentAlive = true;
    }

    // Buggy code from here ...//  this.setState({//      cells[i].alive : currentAlive//  })// ... to here// correct code from here ...
    cells[i].setState({
       alive: currentAlive
    })
    // ... to here
  }
}

Bellow is the complete correct code for the board:

varBoard = React.createClass({

    getInitialState() {
        var array = [];
        for (var i = 0; i < 400; i++) {
            array.push(<Cellkey={i}id={i}cells={array}start={this.props.start} />);
        }

        return {
            cells: array
        };
    },

    generateRandomBoard() {
        for (var i = 0; i < this.state.cells.length ; i++) {
           var currentAlive;
           if(Math.random() < 0.5) {
               currentAlive = false;
           }
           else {
               currentAlive = true;
           }

          cells[i].setState({
             alive: currentAlive
          })
        }
    },

    render() {

        var that = this;

        return (
            <divclassName="board">
                {
                    this.state.cells.map(function(item, i) {
                        return <Cellkey={i}id={i}cells={that.state.cells}start={that.props.start}/>
                    })
                }
            </div>
        );
    }

});

The error you report is not very clear:

SyntaxError: pathToFiles/Board.jsx: Unexpected token, expected , (32:20) while parsing file: pathToFiles/Board.jsx

Yet (32:20) likely means line 32 column 20.

Maybe your current environment is not optimal. I personally use Webpack (for server-side compilation) with source maps (so that it tells me the location of my error): it's some hours to configure the first time, but it's very convenient once it works...

Post a Comment for "How To Generate A Random Starting Position For The Game Of Life?"