변수를 전역 적으로 정의 했으므로 나중에 함수에 액세스 할 수있었습니다. 그러나, 함수 내에서 '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
}
});
});
@dropye 타자를 칠 때 (17 초 차이) 나는 대답을 보지 못했지만 동의합니다. –