How to change the popup window's score to a percentage?

73 views Asked by At

I am trying to create a basic IQ Test with JavaScript, containing only 6 questions.

If you get all questions right and after you click submit, the automatic pop up window will give you a score of 6. However I want it to show a percentage based on the number of correct answers.

Here's what I've tried (you'll see that my problem probably is in the variables):

function calculate()
{
    var x, y, score;
    y;

    x = document.personalinfo.firstname.value;

    score = (y*100)/6;
    window.alert("Hey " + x + ", your score is: " + score);

    if(document.IQTest.Q1[0,1,3,4].checked == true)
        score++;

    if(document.IQTest.Q2[2].checked == true)
        score++;

    if(document.IQTest.Q3[1].checked == true)
        score++;

    if(document.IQTest.Q4[3].checked == true)
        score++;

    if(document.IQTest.Q5[2].checked == true)
        score++;

    if(document.IQTest.Q6[0,2,4].checked == true)
        score++;
}
1

There are 1 answers

11
Blue Sheep On BEST ANSWER

It's just that the code's a bit out of order and you mixed up two variables.

function calculate() {
var x, y, score;

score = 0;
x = document.personalinfo.firstname.value;

if(document.IQTest.Q1[0,1,3,4].checked == true) score++;
if(document.IQTest.Q2[2].checked == true) score++;
if(document.IQTest.Q3[1].checked == true) score++;
if(document.IQTest.Q4[3].checked == true) score++;
if(document.IQTest.Q5[2].checked == true) score++;
if(document.IQTest.Q6[0,2,4].checked == true) score++;

y = (score*100)/6;

window.alert("Hey " + x + ", your score is: " + y);
}