Enjoy an ad free experience by logging in. Not a member yet? Register.

Results 1 to 5 of 5
Thread: Jscript for risk calculator

05182013, 02:20 PM #1
 Join Date
 May 2013
 Posts
 10
 Thanks
 0
 Thanked 0 Times in 0 Posts
Jscript for risk calculator
Hi all,
I'm trying my hand at jscript for the first time (au fait with html and css). I'd like to build a medical risk calculator to sit in a side block on a drupal site. The html form is done.
Input x is the size of an aneurysm and input will be in text and then parsed into ranges (07mm, 710mm and 10+mm)
Input y is location of aneurysm and consists of three options, a, b & c.
Input z is yes / no radio button
Output (risk) should update each time input is changed (no submit button).
Each combination of these variables should produce two linked risk values (% in one year and % in five years). The number of potential outputs is thus 3x3x2 = 18 (so it's not really a calculation, more a lookup). I am imagining some sort of 18faceted list of functions like this:
If x=1 AND y=1 AND z=1 THEN output Risk combo 1
IF x=2 AND y=1 AND z=1 THEN output Risk combo 2
IF x=3 AND y=1 AND z=1 THEN output Risk combo 3
IF x=1 AND y=2 AND z=1 THEN output Risk combo 4
IF x=2 AND y=2 AND z=1 THEN output Risk combo 5
IF x=3 AND y=2 AND z=1 THEN output Risk combo 6
IF x=1 AND y=3 AND z=1 THEN output Risk combo 7
IF x=2 AND y=3 AND z=1 THEN output Risk combo 8
IF x=3 AND y=3 AND z=1 THEN output Risk combo 9
If x=1 AND y=1 AND z=2 THEN output Risk combo 10
IF x=2 AND y=1 AND z=2 THEN output Risk combo 11
IF x=3 AND y=1 AND z=2 THEN output Risk combo 12
IF x=1 AND y=2 AND z=2 THEN output Risk combo 13
IF x=2 AND y=2 AND z=2 THEN output Risk combo 14
IF x=3 AND y=2 AND z=2 THEN output Risk combo 15
IF x=1 AND y=3 AND z=2 THEN output Risk combo 16
IF x=2 AND y=3 AND z=2 THEN output Risk combo 17
IF x=3 AND y=3 AND z=2 THEN output Risk combo 18
How should one best go about performing these functions?
06142013, 12:04 PM
#2
 Join Date
 Jun 2013
 Posts
 1
 Thanks
 0
 Thanked 0 Times in 0 Posts
If you have any doubt you can ask at https://www.udemy.com/javabasicsfo...de=techdisfree. Just join their free online java course and solve your problem.
06142013, 05:21 PM
#3
 Join Date
 Sep 2002
 Location
 Saskatoon, Saskatchewan
 Posts
 16,994
 Thanks
 4
 Thanked 2,662 Times in 2,631 Posts
JScript is not a form of java.
Moving to the Javascript forum.
Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)PHP Code:
header('HTTP/1.1 420 Enhance Your Calm');
06142013, 06:03 PM
#4
 Join Date
 Jun 2002
 Location
 London, England
 Posts
 18,173
 Thanks
 203
 Thanked 2,549 Times in 2,527 Posts
The Javascript (not Java or Jscript) syntax is
if ((x==1) && (y==1) && && (z==1)) {output = "Risk Combo 1"}
But I am confused. You say that input y consists of three options, a, b and c, so where do 1, 2 and 3 come from? Are these radio buttons? You say input z has values "yes" and "no", not 1 and 2.
As an example, you want something like this:
Code:<!DOCTYPE html> <head> </head> <body> <form id = "myform"> SIZE 07mm <input type = "radio" name = "rad1" value = 1 onclick = "update()"> SIZE 710mm <input type = "radio" name = "rad1" value = 2 onclick = "update()"> SIZE over 10mm <input type = "radio" name = "rad1" value = 3 onclick = "update()"> <br><br> LOCATION A <input type = "radio" name = "rad2" value = "a" onclick = "update()"> LOCATION B <input type = "radio" name = "rad2" value = "b" onclick = "update()"> LOCATION C <input type = "radio" name = "rad2" value = "c" onclick = "update()"> <br><br> YES <input type = "radio" name = "rad3" value = "y" onclick = "update()"> NO <input type ="radio" name = "rad3" value = "n" onclick = "update()"> <br><br> THE RESULT <input type = "text" id = "result"> </form> <script type = "text/javascript"> function update() { var A = "None"; var B = "None"; var C = "None"; var f = document.getElementById("myform"); for (var i=0; i<f.rad1.length; i++) { if (f.rad1[i].checked) { A = f.rad1[i].value; } } for (var i=0; i<f.rad2.length; i++) { if (f.rad2[i].checked) { B= f.rad2[i].value; } } for (var i=0; i<f.rad3.length; i++) { if (f.rad3[i].checked) { C = f.rad3[i].value; } } alert ("You have selected " + A + " " + B + " and " + C); // for testing only. alerts as a way of displaying a message are obsolete. if ((A!="None") && (B!="None") && (C!= None")) { // proceed to calculate result as indicated above and display in textbox } } </script> </body> </html>
Quizmaster: Manchuria is a historic region of which country?
Contestant: France
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.
06142013, 07:47 PM
#5
Pardon me, but just a *TINY* bit of SIMPLE arithmetic analysis allows us to see that all of *THIS*:
Can be condensed down to just this:If x=1 AND y=1 AND z=1 THEN output Risk combo 1
IF x=2 AND y=1 AND z=1 THEN output Risk combo 2
IF x=3 AND y=1 AND z=1 THEN output Risk combo 3
IF x=1 AND y=2 AND z=1 THEN output Risk combo 4
IF x=2 AND y=2 AND z=1 THEN output Risk combo 5
IF x=3 AND y=2 AND z=1 THEN output Risk combo 6
IF x=1 AND y=3 AND z=1 THEN output Risk combo 7
IF x=2 AND y=3 AND z=1 THEN output Risk combo 8
IF x=3 AND y=3 AND z=1 THEN output Risk combo 9
If x=1 AND y=1 AND z=2 THEN output Risk combo 10
IF x=2 AND y=1 AND z=2 THEN output Risk combo 11
IF x=3 AND y=1 AND z=2 THEN output Risk combo 12
IF x=1 AND y=2 AND z=2 THEN output Risk combo 13
IF x=2 AND y=2 AND z=2 THEN output Risk combo 14
IF x=3 AND y=2 AND z=2 THEN output Risk combo 15
IF x=1 AND y=3 AND z=2 THEN output Risk combo 16
IF x=2 AND y=3 AND z=2 THEN output Risk combo 17
IF x=3 AND y=3 AND z=2 THEN output Risk combo 18
So if you put all the RiskCombo outputs into a simple array:Code:output( RiskCombo[ x + (y1) * 3 + (z1) * 9 ] );
Then you could do something like:Code:var RiskCombos = [ "You are at very low risk; go enjoy life", "You need to be careful with that aneurysm", ... "You are going to die in 17 seconds" ];
(We use (x1) because the array elements are numbered 0 to 17, not 1 to 18.)Code:<div id="message"></div> ... document.getElementById("message").innerHTML = RiskCombos[ (x  1) + (y1) * 3 + (z1) * 9 ];
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.