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 Coder
    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 (0-7mm, 7-10mm 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 18-faceted 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?

  • #2
    New to the CF scene
    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/java-basics-fo...de=techdisfree. Just join their free online java course and solve your problem.

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    JScript is not a form of java.
    Moving to the Javascript forum.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,898
    Thanks
    203
    Thanked 2,530 Times in 2,508 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 0-7mm <input type = "radio" name = "rad1" value = 1 onclick = "update()">
    SIZE 7-10mm <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.

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,028
    Thanks
    75
    Thanked 4,325 Times in 4,291 Posts
    Pardon me, but just a *TINY* bit of SIMPLE arithmetic analysis allows us to see that all of *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
    Can be condensed down to just this:
    Code:
    output( RiskCombo[ x + (y-1) * 3 + (z-1) * 9 ] );
    So if you put all the RiskCombo outputs into a simple array:
    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"
    ];
    Then you could do something like:
    Code:
    <div id="message"></div>
    ...
    document.getElementById("message").innerHTML = 
        RiskCombos[  (x - 1) + (y-1) * 3 + (z-1) * 9 ];
    (We use (x-1) because the array elements are numbered 0 to 17, not 1 to 18.)
    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.


  •  

    Posting Permissions

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