Reputation: 73
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.
I have the questions working perfectly, however I want to get the score calculated as a percentage.
Here is my Javascript 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 < questions.length; i++) {
askQuestion(questions[i]);
}
// Caclulate score
function scoreTest(answer, questions) {
var score = (answer/questions) * 100;
return score;
}
Here is the HTML code in which the output should display:
<script>
var message = "Your score for the test is " + scoreTest(answer, questions);
document.write("<p>" + message + "</p>")
</script>
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.
Upvotes: 0
Views: 4160
Reputation: 12152
You have to pass the arguments score and questions.length to calculate the percentage where as you only passed variable names in the scoretest
function.
Your code
scoreTest(answer, questions);
What it should be
scoreTest(score, questions.length);
// 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 < questions.length; i++) {
askQuestion(questions[i]);
}
// Caclulate score
function scoreTest(answer, questions) {
var score = (answer/questions) * 100;
return score;
}
var message = "Your score for the test is " + scoreTest(score, questions.length);
document.write("<p>" + message + "%</p>")
Upvotes: 1
Reputation: 8150
The numerator to calculate the score should be the number of questions answered correctly. The denominator should be the total number of questions.
The number of questions answered correctly is score
.
The total number of questions is questions.length
.
So your code can look like:
let message = `Your score for the test is ${(score / questions.length) * 100}%`;
document.write(`<p>${message}</p>`);
Note that this code needs to come after questions
and score
have been declared, and the scoring has occurred.
Upvotes: 0