Cedar - Lizet and Kristin#61
Conversation
beccaelenzil
left a comment
There was a problem hiding this comment.
Great work on this complex React project. Your code is clear and logical. You've clearly met the learning goals around event handling, managing state, and working with 2D arrays. Nice work!
| // When it is clicked on. | ||
| // Then pass it into the squares as a callback | ||
| const setSquareValue = (id) => { | ||
| const newSquares = [...squares]; |
| for (let row of newSquares) { | ||
| for (let square of row) { | ||
| if (square.id === id) { | ||
| if (square.value === '') { |
There was a problem hiding this comment.
Great check that the square is empty!
| if (square.id === id) { | ||
| if (square.value === '') { |
There was a problem hiding this comment.
Consider combing with a compound conditional
| if (currentPlayer === PLAYER_1) { | ||
| setCurrentPlayer(PLAYER_2); | ||
| } else { | ||
| setCurrentPlayer(PLAYER_1); | ||
| } |
There was a problem hiding this comment.
This could be a nice place to refactor with a ternary
| const checkForWinner = (squares) => { | ||
| let newWinner; | ||
| let i = 0; | ||
| while (i < 3) { |
There was a problem hiding this comment.
A while loop works, but can be easy to introduce a bug. Consider using a for loop if you're looping a set number of times.
| } | ||
| } | ||
| } | ||
| checkForWinner(newSquares); |
There was a problem hiding this comment.
Note that this function is called even after there has been a winner, so a player can continue playing after the other player wins. Consider how you can put in a check to update the winner only if there isn't a current winner.
No description provided.