JavaScript JavaScript Loops, Arrays and Objects Tracking Multiple Items with Arrays Build a Quiz Challenge, Part 1 Solution

Kieran Corcoran
Kieran Corcoran
11,188 Points

Build a Quiz Challenge, Part 1 Solution

Hi,

Can any tell me what might be wrong with this code. Does not appear to be converting the response toLowerCase method. If I remove that method it works fine but wonder why the toLowerCase method is causing an issue?

var score = 0;
var question;
var anwser;
var response;
var html;


var questions = [

  ['What is the capital of England', 'London'],
  ['What is the capital of Ireland', 'Dublin'],
  ['What is the capital of Wales', 'Cardiff']

];

for( var i = 0; i < questions.length; i++ ) {
     question = questions[i][0];
     anwser = questions[i][1];
     response = prompt(question);
     if( response.toLowerCase() === anwser ) {
        score += 1;
     }

}

function print(message) {
  document.write(message);
}


html = 'You got ' + score + ' anwsers correct';

print(html);

2 Answers

Steven Parker
Steven Parker
179,662 Points

All of the answers are stored in mixed case (like "London"). So anything that is converted into lower case cannot match them.

But you're halfway there, now just change the stored answers to be lower case (like "london").

Siddharth Pande
Siddharth Pande
9,045 Points
function print(message) {
  document.write(message);
}

var questArray = [
  ["Which gas helps in burning?","OXYGEN"],
  ["Who is the incumbent Prime Minister of India?","NARENDRA MODI"],
  ["Which state is the Capital of India?","DELHI"]
];

//All the main code is in this function:
function quiz (array) { //array is a parameter where argument will be passed.
  var finalHtml = "<p>" //To produce the Html code

  /*To make the 
  wrong answer Html Code*/
  var wrongStr = "<h4>You got these questions wrong: </h4><ol>" 

  var correctAnswer = 0; // Counter for number of correct guesses

  // To Make the correct Answer Html Code
  var correctStr = "<h4> You got these questions correct: </h4><ol>";

  //Engine(loop) where questions are asked and answers are compared by iterating through the loop and using a decision making if statement.
  for(i=0; i < array.length; i += 1) {
    var guess = prompt(array[i][0]);
    if (guess.toUpperCase() === array[i][1]) {
      correctAnswer += 1;
      correctStr += "<li>" + array[i][0] + "</li>"
    } else {
      wrongStr += "<li>" + array[i][0] +"</li>"  
      }
  }

  //completing the Html code 
  finalHtml += "You got " + correctAnswer + 
  " question(s) right. </p> </br>" +
  correctStr + "</ol> </br>" + wrongStr + "</ol>"; 

  //Printing the final Html code on the document
  print(finalHtml);
}

quiz(questArray); //Run the Program