Paper - Katrina Li#52
Conversation
| } | ||
|
|
||
| // check winner helper function | ||
| const checkSame = (x, y, z) => { |
| return 'o' | ||
| } else if (x === 'x' && y === 'x' && z === 'x') { | ||
| return 'x' | ||
| }; |
There was a problem hiding this comment.
If neither of these conditions are matched, the function will return a default of undefined. This will work on line 68 and beyond because undefined is falsey in JavaScript, but explicitly returning false or null makes the expected behavior more clear.
| // Then pass it into the squares as a callbacks | ||
| const updateSqVal = (id) => { | ||
| if (!squares[Math.floor(id/3)][id%3]['value'] && !winner ) { | ||
| const newSq = [...squares]; |
There was a problem hiding this comment.
This works, but if you were attempting to use it to build a copy of squares, it doesn't quite do that. Consider the following:
> const a = [[1,2,3],[4,5,6],[7,8,9]]
> console.log(a)
[ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ]
> const b = [...a]
> console.log(b)
[ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ]
> a[1][1] = "r"
'r'
> console.log(b)
[ [ 1, 2, 3 ], [ 4, 'r', 6 ], [ 7, 8, 9 ] ]
> a == b
false
> a[1] == b[1]
true
In the above example, arrays a & b are using the same inner arrays. This isn't a problem in this particular case, but I just wanted to point out that the [...array] operator is just creating a copy of the outer array, not of the inner arrays.
| const newSq = generateSquares(); | ||
| setSquares(newSq); | ||
| setWinner(WINNER_TBD) |
| } | ||
|
|
||
| const resetGame = () => { | ||
| // Complete in Wave 4 |
There was a problem hiding this comment.
If Player 1 (X) wins and then the user clicks reset, the game will restart with Player 2 (O) as the starting player. If you want Player 1 to be the starting player for each game, this function will need to call setCurrentPlayer(PLAYER_1).
| for (let row of squares) { | ||
| for (let square of row) { | ||
| sqVal.push(square.value) | ||
| } | ||
| } | ||
| if (!sqVal.includes('')) { | ||
| return 'TIE' | ||
| } |
|
Fantastic work on this project! Great code re-use and helper functions (checkSame! 😄 ). Nice work with the reset and tie extensions. This project is green. |
No description provided.