変数をグローバルに定義したので、あとで関数にアクセスできました。しかし、私は関数内から '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
}
});
});
@dropye私は入力している間にあなたの答えを見ませんでした(17秒差)が、私は同意します。 –