1

I wrote a miniature JS quiz before I learned what loops and arrays are, and now I'm trying to tidy it up a bit. The original quiz asks users questions in an alert window, and if they answer incorrectly, it tells them what the correct answer is. If the answer is correct, it tells them they are correct, before proceeding to the next question. It also keeps a tally of how many answers the user gets right or wrong. Here is my original (excessive) code:

var correct = 0;

var answer1 = prompt("What is the capital of England?");
  if (answer1.toUpperCase() === "LONDON") {
    alert("That's correct!")
    correct += 1
  }
  else {
    alert("False. The correct answer is London.")
  }

var answer2 = prompt("What is the capital of France?")
   if (answer1.toUpperCase() === "PARIS") {
    alert("That's correct!")
    correct += 1
  }
  else {
    alert("False. The correct answer is Paris.");
  }

var answer3 = prompt("What is the capital of Canada?")
  if (answer3.toUpperCase() === "OTTAWA") {
    alert("That's correct!");
    correct += 1
  }
  else {
    alert("False. The correct answer is Ottawa.");
  }

document.write("<h1>You answered " + correct + " out of 5 questions correctly.</h1>")

if (correct === 5) {
  document.write("You won a gold star!");
}
  else if (correct >= 3) {
  document.write("You won a silver star.");
}

  else if (correct >= 2) {
  document.write ("You win third place.");
}

else {
  document.write("Sadly, you did not win any stars.");
}

As you can see this is very long and noobish. Here is the re-write I've been working on:

var questions = [
["What is the capital of England?", "LONDON"]
["What is the capital of France?", "PARIS"]
["What is the capital of Canada?", "OTTAWA"]
]

var correctAnswers = 0; 

for (var i = 0; i < questions.length ; i += 1 ) {
question = questions[i][0];
answer = questions[i][1];
response = prompt(question);
response = toUpperCase(response);
if response === answer {
correctAnswers += 1;
}

Where I'm getting a little lost is the structure and placement of the correct answers which are shown to the user by the quiz in an alert window if they answer incorrectly. Would I need to add a third column to the 2 dimensional array and then reference it in the for loop i.e. correctResponse = [i][2]? Or is there an entirely different way I should go about this?

1
  • I know this is just a learning phase for you, but i hope you know you should never store answers or anything valuable in javascript on the client side. Commented May 5, 2016 at 4:38

1 Answer 1

1

You're just missing some commas and your array got messed up. You don't need to complicate the structure of your array anymore. You didn't have commas between your individual array items though.

Essentially you loop through the full array and for each subitem you print out questions[i][0] for the question and questions[i][1] for the answer

var questions = [
["What is the capital of England?", "LONDON"],
["What is the capital of France?", "PARIS"],
["What is the capital of Canada?", "OTTAWA"]
];

var correctAnswers = 0;

for (var i = 0; i < questions.length; i++) {
    var answer = prompt(questions[i][0]);
    if (answer.toUpperCase() == questions[i][1]) {
        alert("Correct!");
        correctAnswers++;
    }
    else {
        alert("incorrect, the correct answer was " + questions[i][1]);
    }
}

Example with custom responses if they get the question right: You just add another item to each question array, and only display that item if they get the question right.

var questions = [
["What is the capital of England?", "LONDON", "You know, where the queen lives"],
["What is the capital of France?", "PARIS", "Remember the eiffel tower?"],
["What is the capital of Canada?", "OTTAWA", "Lot of mooses there"]
];

var correctAnswers = 0;

for (var i = 0; i < questions.length; i++) {
    var answer = prompt(questions[i][0]);
    if (answer.toUpperCase() == questions[i][1]) {
        alert("Correct! " + questions[i][2]);
        correctAnswers++;
    }
    else {
        alert("incorrect, the correct answer was " + questions[i][1]);
    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

Thank you! Do you know how would I add a unique alert for each individual question? i.e. "The correct answer is London. You know, where the Queen lives."
Yeah sure, i'll post an example in a few minutes
@JSJunkie There, I posted an example where if they get it right it will show a custom message for each question
Thanks so much for the help!
So sorry to bother you again but if I wanted to add another question with a numerical answer (i.e. "What is the capital of Thailand?"), where would I factor in the parsInt? Would I have to create a second if statement? i.e. if (parseInt[4][1] === 67000000) { alert("That's correct!"); correct += 1 }?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.