2016-11-27 6 views
0

変数をグローバルに定義したので、あとで関数にアクセスできました。しかし、私は関数内から 'undefined'という値を得ています。それが価値を生み出すのはなぜですか?グローバル変数にアクセスできない

メモ: - グローバル変数は配列である「combineDashes」です。 - 機能を使用したいです。 "をクリックしてください。 1.変数を関数の引数として渡してみました。機能しませんでした。 2.変数が定義されており、関数内に正しい値が含まれていることを確認しました。 。その中では定義された 3.私は別のグローバル変数がうまく機能にそれを作っている確認

combineDashesをグローバル変数として定義されますのはここです:。

$(document).ready(function() {  // upon page load 

     var badGuesses; // reset bad guess counter 
     var theWord;  // defines variable globally 
     var combineDashes; // defines variable globally 
     var letter;  // defines variable globally 
     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", "Z"]; // array of letters to choose 
     $("#lettersRemaining").html(alphabet); // gets elements of the alphabet array and displays on UI 

ここcombineDashesを取得する機能があります定義済み(配列として):

// N E W G A M E B U T T O N C L I C K E D 

    $("#newGame").click(function() { // when user clicks on Start New Game button... 

     $("#status").hide(); // upon game reset hide the status section stating game over 
     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", "Z"]; // reset array of letters to choose from 

     $("#hangmanGuy").html('<img src="img/Hangman-0.png" id="hangmanImg" alt="pic of hangman no limbs" width="144" height="216">'); // reset hangman image 
     badGuesses = 0; // reset guess counter which is used later 

     var wordCollection = ["MANSION", "STATUE", "GORILLA", "NOTEBOOK", "SMARTPHONE", "ILLUSTRATION", "PHOTO", "ELEGANT", "ARBORIST", "KEYBOARD", "CALENDAR", "CAPITAL", "TEXTBOOK", "HORRIBLE", "LIBRARY"]; // array of words the computer will randomly choose from 

     theWord = wordCollection[Math.floor(Math.random()*wordCollection.length)]; // randomly selects a word 
      console.log("theWord is ...."); 
      console.log(theWord); 

     var theWordLength = theWord.length;  // Get number of characters in randomly selected word to know how many dashes to display 
      console.log("theWordLength is ...."); 
      console.log(theWordLength); 

     // D I S P L A Y D A S H E S 

     var combineDashes = []; // creates an array to hold the number of dashes inside the for loop 

     for (var i = theWordLength; i > 0; i--) 
      { 
       combineDashes.push(" - "); // each loop through adds a dash to the array 
      } 

     combineDashes.join(" "); // joins cumulative dashes and converts to a string 

     $("#dashes").html(combineDashes); // displays dashes on UI 
       console.log("combineDashes is..."); 
       console.log(combineDashes); 

    }); 

は、ここで私はcombineDashes変数にアクセスしようとするところだ。

// F O R B A D G U E S S E S 
     if (indices.length === 0) // if bad guess 
      { 
       badGuesses++; // increment bad guess counter 
       $("#status").show(); 
       $("#status").html("Sorry, " + letter + " is incorrect. Try again."); // status message displays 
       console.log("Total badGuesses..."); 
       console.log(badGuesses); 

       // remove guessed letter from alphabet 
       var alphaIndex = alphabet.indexOf(letter);  // gets index of letter in alphabet 
       alphabet.splice(alphaIndex,1); // removes the letter from the alphabet array 
       console.log("alphabet index of letter guessed..."); 
       console.log(alphaIndex); 
       $("#lettersRemaining").html(alphabet); // refreshes letters remaining 

      // display correct hangman image based on how many bad guesses 
        if (badGuesses === 1) 
        { 
         $("#hangmanGuy").html('<img src="img/Hangman-1.png" id="hangmanImg" alt="pic of hangman 1 limb" width="144" height="216">'); 
        } 
        else if (badGuesses === 2) 
        { 
          $("#hangmanGuy").html('<img src="img/Hangman-2.png" id="hangmanImg" alt="pic of hangman 2 limbs" width="144" height="216">'); 
        } 
        else if (badGuesses === 3) 
        { 
          $("#hangmanGuy").html('<img src="img/Hangman-3.png" id="hangmanImg" alt="pic of hangman 3 limbs" width="144" height="216">'); 
        } 
        else if (badGuesses === 4) 
        { 
          $("#hangmanGuy").html('<img src="img/Hangman-4.png" id="hangmanImg" alt="pic of hangman 4 limbs" width="144" height="216">'); 
        } 
        else if (badGuesses === 5) 
        { 
          $("#hangmanGuy").html('<img src="img/Hangman-5.png" id="hangmanImg" alt="pic of hangman 5 limbs" width="144" height="216">'); 
        } 
        else if (badGuesses === 6) 
        { 
          $("#hangmanGuy").html('<img src="img/Hangman-6.png" id="hangmanImg" alt="pic of hangman 6 limbs" width="144" height="216">'); 
          $("#status").html("Game Over. Sorry, you lose. Click Start New Game and try again."); // status message displays 
        } 
      } 
     // F O R G O O D G U E S S E S 
     else 
      { 
       // remove guessed letter from alphabet 
       var alphaIndex = alphabet.indexOf(letter);  // gets index of letter in alphabet 

       alphabet.splice(alphaIndex,1); // removes the letter from the alphabet array 
        console.log("alphabet index of letter guessed..."); 
        console.log(alphaIndex); 

       $("#lettersRemaining").html(alphabet); // refreshes letters remaining 

      // replace dash(es) with letter 

       combineDashes[indices] = letter;  // <--- HUNG UP HERE !!!! 
       console.log(letter); 
       console.log(combineDashes); 


       $("#status").show(); 
       $("#status").html(letter + " is correct! Go again."); // status message displays 


      } 


    }); 

}); 

答えて

1

var combineDashes = []はcombineDashes = []でなければなりませんグローバル変数を設定したい場所に新しいローカル変数を追加します。

+0

@dropye私は入力している間にあなたの答えを見ませんでした(17秒差)が、私は同意します。 –

1

Iamは本当にわからないが、私はあなたが私が推測する次の行

var combineDashes = []; // creates an array to hold the number of dashes inside the for loop 

から「VAR」を削除すべきだと思いますスクリプトはこの名前の新しいローカル変数を作成し、関数の終了後に破棄されます。

+0

作品しかし、インデックス配列に複数の索引が含まれている場合(ハングマンのゲームで、単語に2回以上推測される文字)、ダッシュは正しく更新されません。私はこれについて新しい質問をするべきですか?手紙が一度だけ見つかった場合はうまく動作します。 – TPop

+0

新しい質問を開く必要があると思います。なぜなら、現在のインデックスでは、インデックスが宣言されているか設定されているかわからないからです。 –

関連する問題