1
私の人生のために私はこのウェブページがなぜhtmlのヘッダーを表示しないのか理解できません。私はここに全文を掲載しています。私はjavascriptとhtmlの初心者ですが、機能の仕組みを理解していると思いました。どうやら私はしません。 JSLintによると、最初の "for"ループに問題があるようです。以下にコードを示します。見出しは埋め込まれたjavascriptのHTMLハンマーマンゲームでも表示されません
<!DOCTYPE html>
<html>
<head>
<title>Hangman!</title>
</head>
<body>
<header>
<h1>Hangman!</h1>
</header>
<script>
var words = [
"javascript",
"monkey",
"amazing",
"pancake"
];
var pickWord = function() {
// Return a random word
var word = words[Math.floor(Math.random() * words.length)];
return word;
};
var setupAnswerArray = function(word) {
// Return the answer array
var answerArray = [];
for (var i = 0; i < word.length; i++) {
answerArray[i] = "_";
return answerArray;
};
};
var showPlayerProgress = function (answerArray) {
// Use alert to show the player their progress
alert(answerArray.join(" "));
};
var getGuess = function() {
// Use prompt to get a guess
var guess = prompt("Guess a letter, or click Cancel to stop playing.");
if (guess === null) {
// Exit the game loop
break;
} else if (guess.length !== 1) {
alert("Please enter a single letter.");
};
};
var updateGameState() = function (guess, word, answerArray) {
// Update the answerArray and return a number showing how many times the guess appears in the word
// so remainingLetters can be updated
guess = prompt("Guess a letter, or click Cancel to stop playing.");
for (var j = 0; j < word.length; j++) {
if (word[j] === guess) {
answerArray[j] = guess;
remainingLetters--;
};
};
};
var showAnswerAndCongratulatePlayer = function (answerArray) {
// Use alert to show the answer and congratulate the player
alert(answerArray.join(" "));
alert("Good job! The answer was " + word);
};
var word = pickWord();
var answerArray = setupAnswerArray(word);
var remainingLetters = word.length;
while (remainingLetters > 0) {
showPlayerProgress(answerArray);
var guess = getGuess();
if (guess === null) {
break;
} else if (guess.length !== 1) {
alert("Please enter a single letter.");
} else {
var correctGuess = updateGameState(guess, word, answerArray);
remainingLetters -+ correctGuess;
};
};
showAnswerAndCongratulatePlayer(answerArray);
</script>
</body>
</html>
* if *ブロックの後にセミコロンは必要ありません。 * getGuess *にはreturn文がないので、* undefined *を返します。 – RobG