0

I've hit a brick wall. I'm trying to write my code so that the when the user types a response into the prompt box, their answers are not required to be case sensitive in order for it to be correct. Any suggestions?

Here is my code:

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);

2 Answers2

4

Just compare them with all lowerCase or all upperCase :

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

That way you allow the user to write it any way he wants but still validate correctly

juvian
  • 15,875
  • 2
  • 37
  • 38
1

You could call toLowerCase() or toUpperCase() on both the response and answer variables

if(response.toUpperCase() === answer.toUpperCase()) {
    correctAnswers += 1;
    correct.push(question);
  } else {
    wrong.push(question); 
  }
Richard Hamilton
  • 25,478
  • 10
  • 60
  • 87