Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: Comparison between relative numbers

1. ## 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.

• 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"; // --

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 />
</form>```

• 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?

• Maybe transforming it to hidden fields is the case? Otherwise, set their visibility to "hidden", or set display to "none".

• Originally Posted by Amphiluke
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
•