字母未添加到我的数组中

时间:2019-08-26 11:11:15

标签: javascript arrays

我有一个子手游戏,除了一个部分外,它运行良好。

我有两个数组,一个数组显示要猜的单词,如果猜对了,则添加该单词的字母,第二个数组,如果猜错了,则逐个字母地拼写子手

您的单词数组工作正常,但是如果猜错了,我找不到找到将字母添加到子手数组的方法

playgame('Hello')

//output
/*yourword array*/
H _ _ _ _
/*Hangman array
H _ _ _ _ _ _

如果我猜为'b',则应将下一个字母的hangman添加到hangman arrAy中,因为'b'不在'Hello'中

如果我猜为“ e”,则应在单词数组中添加e,因为“ e”位于“ hello”中

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; // First letter is visible by default
  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

      if (word.indexOf(guess) === -1) {
        remainingLetter--;
      }

    }
    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");
    }
  }
}

playgame('Hello');

1 个答案:

答案 0 :(得分:0)

更新了脚本,以向第二个数组添加字母

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; // First letter is visible by default
    let remainingLetter = hgm.length - 1;
    while (remainingLetters > 0 && remainingLetter > 0) {

        //show the array
        console.log(answerArray.join(" ").toUpperCase());
        console.log(hgArray.join(",").toUpperCase());
        let isGuessCorrect = false
        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--;
                isGuessCorrect = true;
            }
            //add a letter to the hangman string if guessed incorrecty
            //MY PROBLEM IS HERE

            if (word.indexOf(guess) === -1) {
                console.log("Guees ---- " + remainingLetter)
                remainingLetter--;
                break;
            }

        }
        if (!isGuessCorrect) {
            for (var i = 0; i < hgArray.length; i++) {
                if (hgArray[i] == "_") {
                    hgArray[i] = hgm[i];
                    break;
                }
            }
        }
        console.log(answerArray.join(" ").toUpperCase());
        console.log(hgArray.join(",").toUpperCase());
        if (remainingLetter === 0 || remainingLetters === 0)
            break;
    }
    if (remainingLetters === 0) {
        console.log("you win");
    } else if (remainingLetters !== 0) {
        console.log("you lose");
    }

}

样本输出:

H _ _ _ _
H,_,_,_,_,_,_
Guees ---- 6
H _ _ _ _
H,A,_,_,_,_,_
H _ _ _ _
H,A,_,_,_,_,_
Guees ---- 5
H _ _ _ _
H,A,N,_,_,_,_
H _ _ _ _
H,A,N,_,_,_,_
Guees ---- 4
H _ _ _ _
H,A,N,G,_,_,_
H _ _ _ _
H,A,N,G,_,_,_
Guees ---- 3
H _ _ _ _
H,A,N,G,M,_,_
H _ _ _ _
H,A,N,G,M,_,_
Guees ---- 2
H _ _ _ _
H,A,N,G,M,A,_
H _ _ _ _
H,A,N,G,M,A,_
Guees ---- 1
H _ _ _ _
H,A,N,G,M,A,N
you lose