Skip to content

Commit

Permalink
Use two loops to make the squares instead of hardcoding them
Browse files Browse the repository at this point in the history
  • Loading branch information
Sercan Leylek committed Feb 23, 2019
1 parent 83dcaa9 commit 11b4360
Showing 1 changed file with 19 additions and 15 deletions.
34 changes: 19 additions & 15 deletions tic-tac-toe/src/components/Board/Board.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,34 @@ class Board extends React.Component {

renderSquare(i) {
return (<Square
key={i}
value={this.props.squares[i]}
onClick={() => this.props.onClick(i)}
/>
);
}

createTheBoard() {
let board = [];

let counter = 0;
for (let i = 0; i < 3; i++) {
let children = [];
for (let j = 0; j < 3; j++) {
children.push(this.renderSquare(counter));
counter++;
}
board.push(<div className="board-row" key={i}>{children}</div>);
}

return board;
}

render() {

return (
<div>
<div className="board-row">
{this.renderSquare(0)}
{this.renderSquare(1)}
{this.renderSquare(2)}
</div>
<div className="board-row">
{this.renderSquare(3)}
{this.renderSquare(4)}
{this.renderSquare(5)}
</div>
<div className="board-row">
{this.renderSquare(6)}
{this.renderSquare(7)}
{this.renderSquare(8)}
</div>
{this.createTheBoard()}
</div>
);
}
Expand Down

0 comments on commit 11b4360

Please sign in to comment.