-
Notifications
You must be signed in to change notification settings - Fork 72
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Paper - Heidi Zhang #53
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well done!
I left a couple of little style comments but overall things looked great! 😃
const row = Math.floor(squareToUpdate.id/3) | ||
const colume = squareToUpdate.id%3 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a really smart way to find the indexes!
if (squareToUpdate.value === '') { | ||
squareToUpdate.value = currentPlayer | ||
newBoard[row][colume] = squareToUpdate | ||
if (currentPlayer === 'x') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This would be clearer if you used the constants:
if (currentPlayer === 'x') { | |
if (currentPlayer === PLAYER_1) { |
let onedSquares = []; | ||
|
||
for (const row of squares) { | ||
for (const square of row) { | ||
onedSquares.push(square) | ||
} | ||
} | ||
|
||
return onedSquares.map((square) => { | ||
return ( | ||
<Square | ||
value={ square.value } | ||
id={ square.id } | ||
onClickCallback={ onClickCallback } | ||
/> | ||
) | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can use a few built in methods to clean this up.
You can use flat()
instead of your first loop:
let onedSquares = []; | |
for (const row of squares) { | |
for (const square of row) { | |
onedSquares.push(square) | |
} | |
} | |
return onedSquares.map((square) => { | |
return ( | |
<Square | |
value={ square.value } | |
id={ square.id } | |
onClickCallback={ onClickCallback } | |
/> | |
) | |
}) | |
let onedSquares = squares.flat(); | |
return onedSquares.map((square) => { | |
return ( | |
<Square | |
value={ square.value } | |
id={ square.id } | |
onClickCallback={ onClickCallback } | |
/> | |
) | |
}) |
And then JS actually includes the built in method flatMap
that does a flat followed by a map all in one step:
let onedSquares = []; | |
for (const row of squares) { | |
for (const square of row) { | |
onedSquares.push(square) | |
} | |
} | |
return onedSquares.map((square) => { | |
return ( | |
<Square | |
value={ square.value } | |
id={ square.id } | |
onClickCallback={ onClickCallback } | |
/> | |
) | |
}) | |
return squares.flatMap((square) => { | |
return ( | |
<Square | |
value={ square.value } | |
id={ square.id } | |
onClickCallback={ onClickCallback } | |
/> | |
) | |
}) |
No description provided.