-2
所以我有一個HTML頁面,猜猜我的文字遊戲。但是當我輸入我的第一條if語句變量FinalGuess
爲indexOf
它中斷,在那裏我使用SecretWord
該頁的作品,但功能doesnt。有任何想法嗎?javascript有索引問題
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Guessing Game</title>
<script type="text/javascript" >
/* <![CDATA[ */
var SecretWord = "compaq";
var GuessInput = document.forms[0].Guess.text;
var FinalGuess = GuessInput.toLowerCase();
var firstLetter = false;
var b = false;
var c = false;
var d = false;
var e = false;
var f = false;
function checkLetter() {
if (SecretWord.indexOf('c') >= 0) {
firstLetter == true;
}
/*
if (userInput.indexOf('o') >= 0) {
b == true;
}
if (userInput.indexOf('m') >= 0) {
c == true;
}
if (userInput.indexOf('p') >= 0) {
d == true;
}
if (userInput.indexOf('a') >= 0) {
e == true;
}
if (userInput.indexOf('q') >= 0) {
d == true;
}
*/
if (firstLetter == false) {
var letter1 = "*";
} else {
var letter1 = "c";
}
if (b == false) {
var letter2 = "*";
} else {
var letter2 = "o";
}
if (c == false) {
var letter3 = "*";
} else {
var letter3 = "m";
}
if (d == false) {
var letter4 = "*";
} else {
var letter4 = "p";
}
if (e == false) {
var letter5 = "*";
} else {
var letter5 = "a";
}
if (f == false) {
var letter6 = "*";
} else {
var letter6 = "q";
}
var string = letter1 + letter2 + letter3 + letter4 + letter5 + letter6;
document.forms[0].word.value = string;
document.forms[0].Guess.value = "";
document.forms[0].Guess.focus;
alert(GuessInput);
}
/* ]]> */
</script>
</head>
<body onload="checkLetter();">
<form action="" >
Guess the word
<input type="text" name="word" id="word" /><br />
Enter text here
<input type="text" name="Guess" maxlength="1" />
<input type="button" name="Submit" value="Submit" onclick="checkLetter();" />
</form>
</body>
</html>
好吧,所以我將我的變量移到我的函數裏面,它現在可以工作,但我似乎可以得到第一個indexOf爲真 – John
你必須檢查答案的下面,如果答案幫助你投票,如果答案真的是你要找的標記是正確的答案 –