我对这段代码有疑问:
var buyerChoice = prompt("Enter a either apple, orange, or banana:", "");
var fruits = new Array ("apple", "orange", "banana");
for(i=0; i < fruits.length; i++) {
if(buyerChoice === fruits[i]) {
document.write(buyerChoice);
} else if (buyerChoice !== fruits[i]){
document.write("Sorry, " +buyerChoice+ " is out of season.");
break;
}
}
我认为问题出在else-if
语句中,因为每次我输入变量中存在的项目时,它都会返回//appleSorry, apple is out of season
,从而满足这两个条件。
我难住了。我想底线是如何有效地将字符串从提示匹配到数组,测试每个项目以及如果提示字符串不存在如何解析数组。