尝试检查输入变量(sGuess)对我的数组(sportsArray)...
var名称正常
然后当我检查用户针对数组输入的变量(sGuess)时(sportsArray)我总是在控制台中得到以下内容。 循环1返回NaN 循环2返回undefined 循环3完美地返回它的输入
我使用以下javascript代码
var sportsArray = new Array("Football", "Basketball", "Rollerblading", "Hiking", "Biking", "Swimming");
var name;
var score;
var loops;
var sGuess;
// prompts for user name, checks input.
do {
name = prompt("Enter your first name");
}
while (name == "");
// loop for guesses.
for (var i = 1; i <4; i++) {
// prompts user for guess.
sGuess = prompt("Enter your sports guess");
if(sportsArray.indexOf(sGuess) == -1) {
alert("Sorry! Guess Again.");
score -= 5;
}
else {
alert("You are good, try it again!");
score += 5;
}
}
如何修复循环1&amp; 2返回错误?