编辑:使用IF-ELSE语句解决
在我的'if'语句中,或者是||比较不起作用。我之前从这个网站得到了一个答案:这段代码:
if(answers.indexOf(userAnswer.toLowerCase()) !== -1 || answers2.indexOf(userAnswer.toLowerCase()) !== -1)
然而,这对我的目的不起作用(它会看到userAnswer是否在数组中,而如果第3个userAnswer与数组中的第3个答案匹配,我希望语句为真。)
这是我的代码:
var answers = ["hello", "my name is sam", "are you healthy?", "what is your name?", "how much does it cost?", "excuse me", "i'm sorry", "yes, i understand", "no, i don't understand", "just wait please", "where is the toilet?"];
var answers2 = ["hello", "my name is sam", "how are you?", "what's your name?", "how much is it?", "excuse me", "sorry", "yes, all right", "no, i don't understand", "wait, please", "where's the toilet?"];
var totalYes = 0;
var ct = 0;
$("#submmit").click(function() {
function checkAnswers() {
var userAnswer = document.getElementById("b" + ct).value.toLowerCase();
/**aforementioned if statement or comparison wannabe on next line**/
if (userAnswer === answers[ct]) {
totalYes++;
$("#correcto").show();
} else {
$("#incorrecto").show();
if (answers[ct] !== answers2[ct]) {
alert("The correct answer is " + "'" + answers[ct] + "'" + " or " + "'" + answers2[ct] + "'");
} else {
alert("The correct answer is " + "'" + answers[ct] + "'");
}
}
}
checkAnswers();
ct++;
});
答案 0 :(得分:1)
你是否尝试在OR的每一侧放置括号?
if(( answers.indexOf(userAnswer.toLowerCase()) !== -1 ) || (answers2.indexOf(userAnswer.toLowerCase()) !== -1 ) );
也许这可以解决它,我总是不确定js但它肯定不是错误