0
我有我目前使用初始化遊戲格式化,以匹配存儲用戶的輸入值
<h3 id="startGame">Start</h3>
下一個H3元素是我寫
在功能formatText()
JavaScript的我接受用戶輸入的內容,並將整個字符串小寫。然後我把這個單詞的第一個字母大寫,以便它匹配字符串如何寫入數組。在實際上有兩個單詞的情況下,我抓住第二個單詞的第一個字母,並將其大寫以匹配兩個單詞串在數組中寫入的方式。最後,當用戶輸入我要求他們輸入的內容時,他們應該如何編寫它(關於大寫字母)並不重要。所有重要的是他們拼寫正確。
但是,我的問題是,它適用於一個單詞字符串,但不會按照我的意圖大寫第二個單詞。意思是我可以用任何我希望的方式輸入一個字符串(當然拼寫正確),它會解決糾正。我甚至可以輸入兩個字符串並且混淆第一個單詞的大小寫,並且它會解決糾正。但是,當我沒有把第二個單詞大寫時,它總是解決不正確的。
我寫的解決此問題的代碼似乎不起作用,我不知道爲什麼。
var nutrients = [
"Vitamin B6",
"Manganese",
"Vitamin C",
"Fiber",
"Potassium",
"Biotin",
"Copper"
];
function memoNutri() {
var pleaseCopy;
var spaceMarker = " ";
var capitalizeSecondWord;
var firstWord;
var secondWord;
var twoWords;
function ask() {
pleaseCopy = prompt("Enter the following into the text field: " + nutrients[i] + ".");
}
function formatText() {
pleaseCopy.toLowerCase();
pleaseCopy = pleaseCopy[0].toUpperCase() + pleaseCopy.substring(1, pleaseCopy.length);
capitalizeSecondWord = pleaseCopy.substring(spaceMarker + 1, spaceMarker + 2).toUpperCase();
firstWord = pleaseCopy.substring(0, spaceMarker);
secondWord = capitalizeSecondWord + pleaseCopy.substring(spaceMarker + 2, pleaseCopy.length);
twoWords = firstWord + spaceMarker + secondWord;
}
for (i = 0; i < nutrients.length; i++) {
ask();
formatText();
if (pleaseCopy === nutrients[i] || twoWords === nutrients[i]) {
alert("You are correct! " + nutrients[i]);
} else {
alert("That is incorrect");
}
}
}
var startGame = document.getElementById('startGame');
startGame.onclick = memoNutri;