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 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Jul 2009
    Location
    Italy
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Comparison between relative numbers

    I have two tables, as follows:
    TABLE A
    Question 1 Opt1 Opt2 Opt3 Opt 4 Opt5
    Question 2 Opt1 Opt2 Opt3 Opt 4 Opt5
    ....
    Question n Opt1 Opt2 Opt3 Opt 4 Opt5 TOTAL A

    same as above for TABLE B.
    I am using this function:
    Code:
    function Somma() {
      var sum = 0;
      var qn, elem;
      for (i=0; i<65; i++) {
        qn = 'qn'+i; 
        elem = document.getElementById(qn);
        if (elem.checked == true) { sum += Number(elem.value); 
        }    
      }
      document.getElementById('totaleA').value = sum;
    and another function SommaB() for TABLE B, with few changes needed.
    What I get is two autocalculated and populated fields, totaleA and totaleB. Really I do not need to show the totals.
    Since each total can be a positive or negative number I just need a comparison between the two values, with four possible results (++, +-. --, -+).
    An user, after answering all questions, should be able to click a button and get his own profile (out of 4 profiles) displayed in a new page.
    Maybe this is very basic but I have not been able to find an example code of how to do above, also I am just a newbie in javascript and i do not know how to build a function.
    Thanks in advance, and excuse me for my bad (very italian) english.

  • #2
    Regular Coder Amphiluke's Avatar
    Join Date
    Jul 2009
    Posts
    312
    Thanks
    3
    Thanked 89 Times in 89 Posts
    Do you looking for something like this?
    Code:
    <script type="text/javascript">
       var profiles = new Array();
           profiles[0] = "profile1.htm"; // ++
           profiles[1] = "profile2.htm"; // +-
           profiles[2] = "profile3.htm"; // -+
           profiles[3] = "profile4.htm"; // --
    
       function loadProfile() {
          var totA = parseInt(document.getElementById("totaleA").value);
          var totB = parseInt(document.getElementById("totaleB").value);
          var prof;
          if (totA > 0) {
             prof = (totB > 0) ? profiles[0] :  profiles[1];
          } else {
             prof = (totB > 0) ? profiles[2] :  profiles[3];
          }
          window.location.href = prof;
       }
    </script>
    
    ........................
    
    <form action="#">
       <!-- Tables here... -->
       <input type="text" id="totaleA" /><br />
       <input type="text" id="totaleB" /><br />
       <input type="button" value="load profile" onclick="loadProfile();" />
    </form>
    I am still learning English

  • #3
    New to the CF scene
    Join Date
    Jul 2009
    Location
    Italy
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Amphiluke, excellent. It is exactly what I was looking for.
    It works with no problems.

    Is there a way to "hide" the input field for TotaleA and TotaleB? I do not want the numbers shown to the user. Maybe through CSS?

    Thanks again for your help.

  • #4
    Regular Coder Amphiluke's Avatar
    Join Date
    Jul 2009
    Posts
    312
    Thanks
    3
    Thanked 89 Times in 89 Posts
    Maybe transforming it to hidden fields is the case? Otherwise, set their visibility to "hidden", or set display to "none".
    I am still learning English

  • #5
    New to the CF scene
    Join Date
    Jul 2009
    Location
    Italy
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Amphiluke View Post
    Maybe transforming it to hidden fields is the case? Otherwise, set their visibility to "hidden", or set display to "none".
    Hidden fields is fine. Many thanks.

    One more question. I use above SommaA() function in this test, as well as other test tables, getting all the totals I need.
    Basically i repeat the same function as SommaB() SommaC() and so on.
    Is there an elegant and maybe more functional way to do it in a single script, no matter how many sums I must calculate?
    This way I could write an external .js file and call the function as many time as needed in different test pages.


  •  

    Posting Permissions

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