Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    New to the CF scene
    Join Date
    Feb 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question calculate questionnaire %

    hi, i need a help for experts out there.
    i've a set of questionnaire with 10 questions.answer for each question to choose from is 0-6 using radio button.for all the questions answered i need to calculate overall percentage based on the scores.
    i've done the html coding but having problem with javascript..is there any websites i can refer to write a coding or anyone can give example how to do it.
    pls help..

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,915
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Is this not basic mathematics? Simply add up the total values of the checked radio buttons, then divide by 60 (the maximum score) and multiply by 100.

    Or are you asking for people to write the Javascript code for you? You say you are "having problem with Javascript" - show us what you have written so far and doubtless someone will help you correct/improve it.

    This will give you a start:-


    Code:
    <form name= "myform">
    Choose one from this group<br>
    RED <input type = "radio" name = "rad1" value = "0" >
    BLUE <input type = "radio" name = "rad1" value = "1" >
    GREEN  <input type = "radio" name = "rad1" value = "2" >
    YELLOW <input type = "radio" name = "rad1" value = "3">
    BROWN <input type = "radio" name = "rad1" value = "4">
    <br>
    And one from this group<br>
    PINK<input type = "radio" name = "rad2" value = "0" >
    PURPLE<input type = "radio" name = "rad2" value = "1" >
    GREY<input type = "radio" name = "rad2" value = "2" >
    BLACK<input type = "radio" name = "rad2" value = "3" >
    WHITE <input type = "radio" name = "rad2" value = "4">
    <br><br>
    <input type = "button" value = "Calculate Score" onclick ="calc()">
    </form>
    
    <script type = "text/javascript">
    
    var numOfGroups = 2;  // the number of radio button groups - here 2
    
    function calc() {
    var score = 0;
    for (var j=1; j<=numOfGroups; j++) {
    var f = document["myform"]["rad"+j];
    for (var i=0; i<f.length; i++) {
    if (f[i].checked) {
    score += Number(f[i].value);
    }
    }
    }
    
    alert ("You scored "  + score);
    
    }
    </script>
    The rest is trivial.

    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.
    Last edited by Philip M; 02-25-2012 at 08:37 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    New to the CF scene
    Join Date
    Feb 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    here are some of the coding i've done.i m not very sure how to use get element id or name


    <script language="JavaScript">
    // question sets into this array
    var allQuestions = new Array(document.myForm.q1,
    document.myForm.q2,
    document.myForm.q3,
    document.myForm.q4);


    function checkAnswers(){
    var totalScore = 0;


    for (var i in allQuestions) {
    var temp = allQuestions[i];


    for (var j = 0; j < temp.length; j++) {

    //if the correct one is chosen then add 1 to total score
    if (temp[j].value = "" && temp[j].checked ) { /i have a problem here to calculate score
    totalScore++;
    }

    }
    </script>
    Last edited by sxscorpio76; 02-25-2012 at 07:49 AM. Reason: insert script

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,915
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    I thought said you were using radio buttons? Use the code I provided.

    If only one radio is the correct answer, assign a value of 1 to that one and 0 to all the others.
    Last edited by Philip M; 02-25-2012 at 08:39 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #5
    New to the CF scene
    Join Date
    Feb 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    THANKS.ITS WORKING.BUT I HAVE 2 FURTHER QUESTIONS:
    1)HOW TO SET THE DECIMAL TO 1 OR 2 POINT ONLY
    2)HOW TO MAKE SURE ALL THE QUESTIONS ANSWERED B4 PERCENTAGE IS CALCULATED
    why it is not working in firefox?
    HERE IS THE EDITED CODE.

    var numOfGroups = 12;

    function calc() {
    var score = 0;
    for (var j=1; j<=numOfGroups; j++) {
    var f = document["myform"]["ques"+j];
    for (var i=0; i<f.length; i++) {
    if (f[i].checked) {
    score += Number(f[i].value /72 *100);
    }
    }
    }
    //confused here..do i need to create new function somenthing like this
    function showScore() {
    if(answered != answers.length) {
    alert("You have not answered all of the questions yet!");
    return false; OR
    }

    //if numOfGroups != 12 {

    alert("You have not answered all of the questions yet!");
    }
    else{

    alert ("Your Score is " + score);
    return false;


    }
    </script>
    Last edited by sxscorpio76; 02-25-2012 at 03:54 PM. Reason: code edit

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,915
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    a) use .toFixed*(2)

    alert ("Your Score is " + score.toFixed(2));

    b) Simply add up the number of answers given in a new variable count and then compare with the number of radio groups.

    score += Number(f[i].value);
    count ++;

    c) score += Number(f[i].value /72 *100);
    Do not attempt to calculate the percentage correct until you have added up all the scores.

    d) //if numOfGroups != 12 { That is simply silly. The number of radio groups is the number you have specified.
    Last edited by Philip M; 02-25-2012 at 05:34 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #7
    New to the CF scene
    Join Date
    Feb 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    i dont understand (b) do u mean i need to create var count or something like that ?
    i've seen many programs use document.getElementbyId function in javascript.is it really compulsary to use it or as long the script works and can be executed that will be fine?

    b) Simply add up the number of answers given in a new variable count and then compare with the number of radio groups.

    sorry i m really new to javascript..thanks

  • #8
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,915
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by sxscorpio76 View Post
    i dont understand (b) do u mean i need to create var count or something like that ?
    sorry i m really new to javascript..thanks
    Yes! If you cannot do that you are in serious trouble!!

    I do not understand what you mean by document.getElementById() being "compulsory". But form fields should be assigned names. There is not much point in assigning an id to a form field as only those with names are passed to the server. You address a form field as document.formname.elementname.value.

    To be candid, you seem to be making very heavy weather of this. It is not really in your best interests that others do your all or most homework for you. Your teacher may gain a false and exaggerated idea of your programming capabilities and so not offer you the support you need.
    Also, if you hand in other people's work which you do not completely understand, then you will start to fall behind and your difficulties will increase. You have had a lot of help - and I'll bet that you do not understand the code I gave you.

    But I get the idea that this assignment is too difficult and unsuitable for a person of your skill level - assuming that you have been paying attention in class.
    Last edited by Philip M; 02-25-2012 at 05:59 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #9
    New to the CF scene
    Join Date
    Feb 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    i m dealing with online questionnaire. done coding for html. now writing javascript to calculate percentage thats why i asked u about get element by id.i try to use it in this way

    var One = document.getElementById('ques1').value;
    var Two = document.getElementById('ques2').value;
    var Three = document.getElementById('ques3').value;---->till ques 12
    is there any other way by not listing all the questions as above? even if i use array i still need to list all the questions like shown below

    var allQuestions = new Array(document.myForm.ques1,
    document.myForm.ques2,
    document.myForm.ques3,
    document.myForm.ques4);
    Last edited by sxscorpio76; 02-26-2012 at 10:27 AM. Reason: code edit

  • #10
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,915
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    You have not read what I told you in Post#8. I have provided you with the code to calculate the score according to the radio button value. Surely you can convert that into a percentage?

    Code:
    alert ("Your Score is " + score.toFixed(2));
    var percentage = score / maximumPossibleScore *100;
    alert ("Your percentage correct was " + percentage.toFixed(2) +"%");
    There is no use or requirement for document.getElementById() in this exercise. To be candid, I do not understand what you are trying to do with the code you have posted. As I say, I fear that you are out of your depth.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #11
    New to the CF scene
    Join Date
    Feb 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    FYI i m learning web programming online.i meet my tutor only 4 time or only 8 hours throughout the semester.during my face to face tutorial my most time spent on the incomplete module provided by the institute.how could u learn programming in 8 hours?? so i need to reaad a lot of tutorials online to get more knowledge in writing javascript.now at least i've master the html coding but having problem with js.i m not asking u to do my homework anyway,just need a right guide from ppl like u to finish the assignment.
    Your code really helpfull and i m trying to understand it better.if possible pls indicate what the code means for my understanding.

    ok.earlier i asked u about how to prompt user to answer all the questions b4 proceeding with percentage calculation. but i forgot to tell u i've 2 parts where part 1 questions have radio button and drop down list while part 2 have 12 questions with radio button.
    So how to validate this radio button and drop down list? is creating new variable count still applicable for this?

    thanks


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •