我的猜词游戏打破了什么?

时间:2017-04-27 18:34:07

标签: javascript

新的编码,有人可以告诉我什么是错的,我试图创建一个猜词一字游戏,它之前正在工作,但改变了一对for循环,并没有工作

var guessLetters
var currentWord
var guesses
var wrongGuesses
var correctGuesses
var currentWord 
var badLetters
var index

var guessWords = ["baseball","basketball","football","soccer","volleyball","golf","frisbee","cricket","bowling","billards","polo","pool","swimming","gymnastics","badminton","handball","lacrosse","surfing","tennis","softball","rugby","ice skating","ice hockey","track and field","squash","table tennis", "archery","judo","water polo","field hockey","four square","rowing","snowboarding","skiing","climbing","running","wrestling","horse racing","fencing","curling","diving"]

function startGame(){
  currentWord = guessWords[Math.floor(Math.random()*guessWords.length)];
  document.getElementById("Random Test").innerHTML = currentWord;
  document.getElementById("demo").innerHTML = "The blanks represent letters in the hidden word are here: " + secretBlanks(currentWord);
  guessLetters = []
  badLetters = []
  index = []
  guesses = 0
}
function answer() {
var currentGuess = document.getElementById("currentGuess").value;
document.getElementById("guessLetter").innerHTML= currentGuess;


  for (i = 0; i < guessLetters.length; i++) {
    if (currentGuess === guessLetters[i]){
      document.getElementById("Guess Repeat").innerHTML ="You already guessed that silly!";
  return}
 }
  document.getElementById("Guess Repeat").innerHTML = "Valid Guess";
   guessLetters.push(currentGuess);
  document.getElementById("Already guessed").innerHTML = guessLetters;

var match=0;  
for (j = 0; j < currentWord.length; j++){
  if (currentGuess=currentWord[j]){
    index.push(j);
    match++;
  }
 }
  if (match=0){
    badLetters.push(currentGuess)
    document.getElementById("Guess Result").innerHTML = "Incorrect Guess";
    document.getElementById("Incorrectguess").innerHTML = "These are your incorrect guesses" + badLetters;
  } else{
    document.getElementById("Guess Result").innerHTML = "Correct Guess";
    document.getElementById("demo").innerHTML = "The blanks represent letters in the hidden word are here: " + secretBlanks(currentWord);
  }
}


function secretBlanks(secretWord) {
  correctLettersOUT = "";
for (var i = 0; i < secretWord.length; i++) {
  if(index.includes(i)){
    correctLettersOUT+=(secretWord[i])
    correctLettersOUT+=(" ")
  }else{
    correctLettersOUT+=("_ ");
  }


} return correctLettersOUT;
}

1 个答案:

答案 0 :(得分:0)

你忘记了很多分号。这里采用这个版本:

&#13;
&#13;
var guessLetters;
var currentWord;
var guesses;
var wrongGuesses;
var correctGuesses;
var currentWord ;
var badLetters;
var index;

var guessWords = ["baseball","basketball","football","soccer","volleyball","golf","frisbee","cricket","bowling","billards","polo","pool","swimming","gymnastics","badminton","handball","lacrosse","surfing","tennis","softball","rugby","ice skating","ice hockey","track and field","squash","table tennis", "archery","judo","water polo","field hockey","four square","rowing","snowboarding","skiing","climbing","running","wrestling","horse racing","fencing","curling","diving"];

function startGame(){
  currentWord = guessWords[Math.floor(Math.random()*guessWords.length)];
  document.getElementById("Random Test").innerHTML = currentWord;
  document.getElementById("demo").innerHTML = "The blanks represent letters in the hidden word are here: " + secretBlanks(currentWord);
  guessLetters = [];
  badLetters = [];
  index = [];
  guesses = 0;
}
function answer() {
var currentGuess = document.getElementById("currentGuess").value;
document.getElementById("guessLetter").innerHTML= currentGuess;


  for (i = 0; i < guessLetters.length; i++) {
    if (currentGuess === guessLetters[i]){
      document.getElementById("Guess Repeat").innerHTML ="You already guessed that silly!";
  return;
  }
 }
  document.getElementById("Guess Repeat").innerHTML = "Valid Guess";
   guessLetters.push(currentGuess);
  document.getElementById("Already guessed").innerHTML = guessLetters;

var match=0;  
for (j = 0; j < currentWord.length; j++){
  if (currentGuess=currentWord[j]){
    index.push(j);
    match++;
  }
 }
  if (match=0){
    badLetters.push(currentGuess);
    document.getElementById("Guess Result").innerHTML = "Incorrect Guess";
    document.getElementById("Incorrectguess").innerHTML = "These are your incorrect guesses" + badLetters;
  } else{
    document.getElementById("Guess Result").innerHTML = "Correct Guess";
    document.getElementById("demo").innerHTML = "The blanks represent letters in the hidden word are here: " + secretBlanks(currentWord);
  }
}


function secretBlanks(secretWord) {
  correctLettersOUT = "";
for (var i = 0; i < secretWord.length; i++) {
  if(index.includes(i)){
    correctLettersOUT+=(secretWord[i]);
    correctLettersOUT+=(" ");
  }else{
    correctLettersOUT+=("_ ");
  }


} return correctLettersOUT;
}
&#13;
&#13;
&#13;