我撞到了一堵砖墙。我正在尝试编写我的代码,以便当用户在提示框中键入响应时,他们的答案不需要区分大小写以使其正确。有什么建议吗?
这是我的代码:
var questions = [
["What solar system do we live in?", "Milky Way"],
["How many ounces are in a cup?", "16"],
["What type of cellphone is currently the most popular throughout the world?", "iPhone"],
["What's Chicago's tallest building?", "Willis Tower"],
["What is the name of the Chicago White Sox' new stadium?", "Guaranteed Rate"]
];
var correctAnswers = 0;
var question;
var answer;
var response;
var html;
var correct = [];
var wrong = [];
function print(message) {
var outputDiv = document.getElementById("output");
outputDiv.innerHTML = message;
}
function buildList(arr) {
var listHTML = "<ol>";
for(var i=0;i<arr.length;i++) {
listHTML += "<li>" + arr[i] + "</li>";
}
listHTML += "</ol>";
return listHTML;
}
for(var i=0;i<questions.length;i++) {
question = questions[i][0];
answer = questions[i][1];
response = prompt(question);
if(response === answer) {
correctAnswers += 1;
correct.push(question);
} else {
wrong.push(question);
}
}
html = "You got " + correctAnswers + " question(s) right.";
html += "<h2>You got these questions correct:</h2>";
html += buildList(correct);
html += "<h2>You got these questions wrong:</h2>";
html += buildList(wrong);
print(html);
答案 0 :(得分:3)
只需将它们与所有lowerCase或所有upperCase进行比较:
if(response.toLowerCase() === answer.toLowerCase()) {
这样你就允许用户以他想要的任何方式编写它但仍能正确验证
答案 1 :(得分:0)
您可以在响应和回答变量上调用toLowerCase()
或toUpperCase()
if(response.toUpperCase() === answer.toUpperCase()) {
correctAnswers += 1;
correct.push(question);
} else {
wrong.push(question);
}