PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, July 26, 2022

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

 July 26, 2022     dreamweaver, javascript, onsubmit, popupwindow, testing     No comments   

Issue

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++;
}

Solution

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);
}


Answered By - Blue Sheep
Answer Checked By - Pedro (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing