我的第一个使用Javascript的猜字母游戏

时间:2018-11-10 02:59:52

标签: javascript arrays

我对JS非常陌生,我正在尝试完成我的作业。这是一个游戏,用户尝试猜测计算机在想的字母。我希望每次推入一个字母时猜测的数量都减少,但是它从10变为9到-1,然后保持在-1。没有人有任何提示吗?提前非常感谢您!

var alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
  "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y",
  "z"
];
var wins = 0;
var losses = 0;
var guessesNum = [10];
var guessChoice = [];
var directionsText = document.getElementById("directions-text");
var winsText = document.getElementById("wins-text");
var lossesText = document.getElementById("losses-text");
var guessesLeftText = document.getElementById("Guesses-left-text");
var guessesSoFarText = document.getElementById("Guesses-so-far-text");
var computerChoice = alphabet[Math.floor(Math.random() *
  alphabet.length)];
console.log(computerChoice)


document.onkeypress = function(event) {
  var userGuess = event.key;



  if (userGuess === computerChoice) {
    wins++;
    guessesNum = 9;
    guessChoice = [];
    winsText.textContent = 'Wins: ' + wins;

  } else {
    if (guessChoice.includes(userGuess)) {} else {
      guessesNum--;
      guessChoice.push(userGuess);
      guessesLeftText.textContent = "Guesses Left: " + guessesNum;
      guessesSoFarText.textContent = "Guesses So Far: " + guessChoice;
    }
  }
  if (guessesNum = 0) {
    guessesNum++;
    losses++;
    guessChoice = [];
    lossesText.textContent = "Losses: " + losses;
  }
}
<div id="directions-text"></div>
<div id="wins-text"></div>
<div id="losses-text"></div>
<div id="Guesses-left-text"></div>
<div id="Guesses-so-far-text"></div>

0 个答案:

没有答案