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: Jscript for risk calculator

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

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

• JScript is not a form of java.
Moving to the Javascript forum.

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

<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++) {
}
}
for (var i=0; i<f.rad2.length; i++) {
}
}
for (var i=0; i<f.rad3.length; i++) {
}
}

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

• 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.)

•

#### Posting Permissions

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