Open helenzhou6 opened 5 years ago
in progress
Figured out ✨
Previously when trying to use prevState
it didn't work since I didn't realise the state.displayArray
would not always update (only updates if the letter exists in the hidden word - as part of the check()
function)
So instead this works:
checkExist = () => {
this.setState({
displayArray: check(
this.state.randomWordGenerator,
this.state.letterInput,
this.state.displayArray)
});
this.setState({ letterInput: "" });
if (!this.state.displayArray.includes(this.state.letterInput)) {
this.setState({
lives: this.state.lives - 1
});
}
};
Where the lives - 1
when the new displayArray
(after it's updated) does not include the letterInput
https://github.com/fac-15/hangman/blob/cfb092c5140917637031a48952d3f61736073a3d/form.js#L51