title: How to calculate a percentage score for a quiz in JavaScript?
body: <p>I've created a quiz in Javascript and should output an score in HTML. The user is prompted the quiz questions and afterwards, their score should be outputted into an HTML file. </p>
  
  <p>I have the questions working perfectly, however I want to get the score calculated as a percentage. </p>
  
  <p>Here is my Javascript code:</p>
  
  <pre><code>// Declare the "score" variable
  var score = 0;
  // Create the questions array
  var questions = [
  ["T or F: Two plus two is ten."],
  ["T or F: George Washington was the first U.S.president."],
  ["T or F: Al Gore is our current Vice President."],
  ["T or F: Two plus two is four."],
  ["T or F: You are not an alien from Mars."]
  ];
  // Create the answer key array
  var answer_key = [
  ["F"],
  ["T"],
  ["F"],
  ["T"],
  ["T"]
  ];
  // Ask each question
  function askQuestion(question) {
    var answer = prompt(question[0], "");
    if (answer.toUpperCase() == answer_key[i]) {
      alert("Correct!");
      score++;
    } else if (answer==null || answer=="") {
      alert("You must enter T or F!");
      i--;
    } else {
      alert("Sorry. The correct answer is " + answer_key[i]);
    }
  }
  for (var i = 0; i &lt; questions.length; i++) {
    askQuestion(questions[i]);
  }
  
  // Caclulate score
  function scoreTest(answer, questions) {
  var score = (answer/questions) * 100;
  return score;
  }
  </code></pre>
  
  <p>Here is the HTML code in which the output should display:</p>
  
  <pre><code>&lt;script&gt;
  var message = "Your score for the test is " + scoreTest(answer, questions);
  document.write("&lt;p&gt;" + message + "&lt;/p&gt;")
  &lt;/script&gt;
  </code></pre>
  
  <p>If the output/function were working, it should display "Your score for the test is 80%", assuming 4/5 questions were answered correctly for example.</p>