I need to add the letters H,A,N,G,M,A,N one at a time if a user guesses the word wrong
I have tried using a loop but am unsure how to do it
function playgame(yourword) {
//yourword array
/*********************/
let word = yourword.toUpperCase();
let letters = word.split("");
var answerArray = [letters[0]];
for (var i = 1; i < letters.length; i++) {
answerArray[i] = "_";
}
let answer = answerArray.join();
//hangman array
/*********************/
let hgm = ["H", "A", "N", "G", "M", "A", "N"];
let hgArray = [hgm[0]];
for (var i = 1; i < hgm.length; i++) {
hgArray[i] = "_";
}
//loop
let remainingLetters = word.length;
let remainingLetter = hgm.length;
while (remainingLetters > 0 & remainingLetter > 0) {
//show the array
console.log(answerArray.join(" ").toUpperCase());
console.log(hgArray.join(",").toUpperCase());
let guess = prompt("guess a letter").toUpperCase();
//add a letter if guessed correctly
for (var i = 0; i < letters.length; i++) {
if (word[i] === guess) {
answerArray[i] = guess;
remainingLetters--;
}
//add a letter to the hangman string if guessed incorrecty
//MY PROBLEM IS HERE
for (var h = 0; h < hgm.length; h++) {
if (word[h] !== guess) {
remainingLetter--;
}
//TO HERE
}
}
console.log(answerArray.join(" ").toUpperCase());
console.log(hgArray.join(",").toUpperCase());
if (remainingLetters === 0) {
console.log("you win");
} else if (remainingLetters !== 0) {
console.log("you lose");
}
}
}
I have tried using a loop the same way I did to increment the next letter but I was not sure how to go about doing it.
Please help out
You do not need a loop since you only want to insert a letter at the current position.
function playgame(yourword) {
//yourword array
/*********************/
let word = yourword.toUpperCase();
let letters = word.split("");
var answerArray = [letters[0]];
for (var i = 1; i < letters.length; i++) {
answerArray[i] = "_";
}
let answer = answerArray.join();
//hangman array
/*********************/
let hgm = ["H", "A", "N", "G", "M", "A", "N"];
let hgArray = [hgm[0]];
for (var i = 1; i < hgm.length; i++) {
hgArray[i] = "_";
}
//loop
let remainingLetters = word.length - 1;
let remainingLetter = hgm.length;
let currentHangmanPosition = 1;
while (remainingLetters > 0 & remainingLetter > 0) {
//show the array
console.log(answerArray.join(" ").toUpperCase());
console.log(hgArray.join(",").toUpperCase());
let guess = prompt("guess a letter").toUpperCase();
//add a letter if guessed correctly
if (letters[word.length - remainingLetters] == guess) {
answerArray[word.length - remainingLetters] = guess;
remainingLetters--;
}else{
hgArray[currentHangmanPosition] = hgm[currentHangmanPosition];
currentHangmanPosition++;
}
console.log(answerArray.join(" ").toUpperCase());
console.log(hgArray.join(",").toUpperCase());
if (remainingLetters === 0) {
console.log("you win");
} else if (currentHangmanPosition == hgm.length) {
console.log("you lose");
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.