I want to made a hangman game in React.js and I have a function who take a random word among a list of word, I want to split this word and replace each letter by a "_ " but when I set the setState in the .map, it take only one "_" and not as many times as letters.
Someone can help me ?
const words = [...this.state.wordList];
const length = words.length;
const random = Math.floor(Math.random() * length);
const word = this.state.wordList[random].word;
const splitedWord = word.split("");
splitedWord.map(( letter, index ) => {
const hiddenLetter = letter.replace(/./, "_");
this.setState({usedWord: hiddenLetter});
}
_, you need to add a global flag to the regex:letter.replace(/./g, "_");(notice theg). But it makes little sense to callsetStatein a.map…_____? Just create that string. Something likeconst splittedWord = new Array(word.length + 1).join("_");Then set the state to that.setState()in every iteration of a loop. It's not efficient