Thread: Jscript for risk calculator View Single Post
06-14-2013, 08:47 PM   PM User | #5
Old Pedant
Supreme Master coder!

Join Date: Feb 2009
Posts: 24,955
Thanks: 75
Thanked 4,308 Times in 4,275 Posts
Pardon me, but just a *TINY* bit of SIMPLE arithmetic analysis allows us to see that all of *THIS*:
Quote:
 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.