Greetings, website visitors! I am facing a major dilemma with my hangman game.
Here's the deal - I have a word bank:
wordBank = ["pizza", "pie", "cookie", "candy", "salad", "chicken", "pork", "burger", "fries"]
To start off, I randomly select a word from the word bank and convert it into an array:
randomChosenWord = wordBank[round(random(wordBank.length - 1))].split('')
Additionally, I have an array that contains placeholders equal to the length of the chosen word. When I click on one of the letter buttons, it replaces the corresponding placeholder in this array with the selected letter:
this.click = function() {
if (mouseX > this.x - 10 && mouseX < this.x + 10 && mouseY > this.y - 10 && mouseY < this.y + 10) {
if (randomChosenWord.includes(this.letter)) {
lettersChosen.splice(randomChosenWord.indexOf(this.letter), 1, this.letter)
}
For instance, let's say the random word is "Pizza."
The array representing the chosen letters will initially be displayed as "- - - - -" (matching the length of Pizza).
If I press the 'z' button, the placeholders at the respective indexes where 'z' appears in the word will be replaced. So, it would show up as "- - z - -".
While this functionality is effective, I aim to replace all placeholders within index positions that contain 'z,' showcasing it as "- - z z -".
Could anyone advise me on identifying duplicated elements in an array and substituting them?