2016-11-27 5 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 가져 곳 정의 됨 (배열로) :

나는 combineDashes 변수에 액세스하려고 할 경우 다음 691,363,210
// 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); 

    }); 

는 다음과 같습니다

// 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

스피 정말 확실하지를하지만 난 당신이

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

내가 추측 다음 줄에서 'var에'를 삭제해야한다고 생각 스크립트가이 이름으로 새 로컬 변수를 만듭니다. 함수가 끝난 후에 버려집니다.

+0

작품! 그러나 인덱스 배열에 행맨 게임이 여러 개 포함되어 있고 단어에 두 번 이상 추측 된 문자가 포함되어 있으면 대시가 올바르게 업데이트되지 않습니다. 이 문제에 대한 새로운 질문을해야합니까? 글자가 한 번만 발견되면 잘 작동합니다. – TPop

+0

새로운 질문을 열어야한다고 생각합니다. 왜냐하면 현재의 질문에서 인덱스가 선언되거나 설정된 곳을 보지 못하기 때문입니다. –

관련 문제