indexOf 没有返回正确的索引
indexOf not returning correct index
回复:刽子手游戏
Objective:用户猜完一个字母后,检索该字母在单词中的索引。
问题:返回的数字不是单词的正确索引。倒数第三行代码。
我是新手所以请放轻松:)这太难了!
// JavaScript Document
$(document).ready(function() { // upon page load
var badGuesses; // reset bad guess counter
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
// 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
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
var 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
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
});
// G U E S S L E T T E R
$("#guessLetter").click(function(theWord) { // when user clicks on the Guess Letter button pass in theWord value ....
var letter = $("#theLetter").val(); // gets the letter the user is guessing
console.log("letter is ...");
console.log(letter);
// Is the letter a good or bad guess?
var letterContained = theWord.toString().indexOf(letter); // <-- NOT WORKING!! returns index from theWord for the letter guessed; -1 means bad guess
console.log("letterContained is...");
console.log(letterContained);
});
});
上面的评论都是这样,但问题是 theWord 需要在与 badGuesses 相同的级别声明(并且在 newGame 中分配但不声明,就像 badGuesses 一样)并作为参数删除给 guessLetter .然后 guessLetter 得到了 theWord 的期望值,并且 indexOf 调用起作用了。
回复:刽子手游戏
Objective:用户猜完一个字母后,检索该字母在单词中的索引。
问题:返回的数字不是单词的正确索引。倒数第三行代码。
我是新手所以请放轻松:)这太难了!
// JavaScript Document
$(document).ready(function() { // upon page load
var badGuesses; // reset bad guess counter
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
// 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
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
var 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
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
});
// G U E S S L E T T E R
$("#guessLetter").click(function(theWord) { // when user clicks on the Guess Letter button pass in theWord value ....
var letter = $("#theLetter").val(); // gets the letter the user is guessing
console.log("letter is ...");
console.log(letter);
// Is the letter a good or bad guess?
var letterContained = theWord.toString().indexOf(letter); // <-- NOT WORKING!! returns index from theWord for the letter guessed; -1 means bad guess
console.log("letterContained is...");
console.log(letterContained);
});
});
上面的评论都是这样,但问题是 theWord 需要在与 badGuesses 相同的级别声明(并且在 newGame 中分配但不声明,就像 badGuesses 一样)并作为参数删除给 guessLetter .然后 guessLetter 得到了 theWord 的期望值,并且 indexOf 调用起作用了。