I have been attempting to search an array for a specific string using a For loop, iterating through each index of the array based on its length. The expected behavior is that if the string is found, it should display that string (which in this scenario is a letter). However, even when the letter is present, the output does not get updated as intended.
For instance, take the word DOG. Initially, the output array would appear as '▢ ▢ ▢'. If I were to guess 'D', the output array should then update to 'D ▢ ▢'. Unfortunately, this update does not occur.
Below is the relevant code snippet:
function encryptLetters() {
instructions.style.display = "none";
for (i = 0; i < magicyMagic.length; i++) {
if (magicyMagic.substring(i, i + 1) == " ") {
output[i] = "   ";
} else if (magicyMagic.substring(i, i + 1) == ".") {
output[i] = ".";
} else {
output[i] = " ▢"
}
}
document.getElementById("mysteryWord").innerHTML = output.join("");
firstTurn();
}
function player1() {
console.log("Player 1 is going.");
nowGoing = 1;
player1Elements.style.display = "block";
alert(playerName1 + " it is your turn. What do you guess?");
ask();
}
function ask() {
console.log("Prompting");
var guess = prompt("What letter would you like to guess?");
var playerGuess = guess.toUpperCase();
console.log(playerName1 + " guessed " + playerGuess);
checkGuessType(playerGuess);
}
function checkGuessType(playerGuess) {
console.log("Checking type...");
if (playerGuess.length > 1) {
console.log(playerName1 + " guessed a word.");
} else if (playerGuess == ...
In the developer console, only the following messages are visible:
"The first player's name is Test1."
"The second player's name is Test2."
"The player(s) have been informed."
"The word is UGANDA"
"Player 1 is going."
"Prompting"
"Test1 guessed A"
"Checking type..."
"Test1 guessed a vowel."
"Checking if letter is present..."
"Ding ding ding!"
"Ding ding ding!"
"A has been added to the list. Players cannot guess this letter again."
Even though the console log statements related to updating the output array appear, the array itself remains unchanged. How can this issue be resolved?
P.S. Any variables not explicitly shown in the provided code snippet are most likely global variables, but their absence shouldn't impact the solution.
EDIT: Here is a list of all relevant variables - note that all array values are capitalized, yet the issue persists despite this fact.
var playerName1;
var playerName2;
var nowGoing = 0;
var magicyMagic;
var isCorrect = false;
// ____ Trees (types of trees - 1)
var array1Trees = ["OAK", "ELM", "PINE", "BIRCH", "SPRUCE", "ASPEN", "MAPLE", "CEDAR", "REDWOOD", "CHERRY"];
...