Javascript 如何使我的用户响应不区分大小写?

Javascript 如何使我的用户响应不区分大小写?,javascript,function,for-loop,Javascript,Function,For Loop,我撞到了砖墙。我试图编写代码,这样当用户在提示框中键入响应时,他们的答案就不需要区分大小写才能正确。有什么建议吗 这是我的密码: 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

我撞到了砖墙。我试图编写代码,这样当用户在提示框中键入响应时,他们的答案就不需要区分大小写才能正确。有什么建议吗

这是我的密码:

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);
var问题=[
[“我们生活在哪个太阳系?”,“银河系”],
[“一个杯子里有多少盎司?”,“16”],
[“目前全球最流行的手机类型是什么?”,“iPhone”],
[“芝加哥最高的建筑是什么?”,“威利斯大厦”],
[“芝加哥白袜队新体育场的名称是什么?”,“保证费率”]
];
var=0;
var问题;
var回答;
var反应;
var-html;
var-correct=[];
var错误=[];
功能打印(消息){
var outputDiv=document.getElementById(“输出”);
outputDiv.innerHTML=消息;
}
函数构建列表(arr){
var listHTML=“”;

对于(var i=0;i只需将它们与所有小写或大写进行比较:

if(response.toLowerCase() === answer.toLowerCase()) {

这样,您可以允许用户以任何方式编写,但仍能正确验证

只需将它们与所有小写或大写进行比较:

if(response.toLowerCase() === answer.toLowerCase()) {

通过这种方式,您可以允许用户以任何方式编写它,但仍能正确验证

您可以对响应和应答变量调用
toLowerCase()
toUpperCase()

if(response.toUpperCase() === answer.toUpperCase()) {
    correctAnswers += 1;
    correct.push(question);
  } else {
    wrong.push(question); 
  }

您可以对response和answer变量调用
toLowerCase()
toUpperCase()

if(response.toUpperCase() === answer.toUpperCase()) {
    correctAnswers += 1;
    correct.push(question);
  } else {
    wrong.push(question); 
  }

将所有内容转换为小写,然后比较
.toUpperCase()
.toUpperCase()
以转换答案
if(response.toLowerCase()===answer.toLowerCase()){
将所有内容转换为小写,然后比较
.toUpperCase()
.toLowerCase())
要转换答案
如果(response.toLowerCase()==answer.toLowerCase()){
可能重复@D.Hawkins没有问题,如果您的问题已经解决,请随时接受答案solved@D.Hawkins没问题,如果您的问题已经解决,请随意接受答案