Im just looking for a little bit of input or possibly another approach to what I'm trying to accomplish.
I am in the early stages of creating a small application/activity to simulate taking someone's blood pressure. My base logic is sound, and the seems to be functional, however, it feels like ive pretty much "hacked" it to get it to work.
So here is the code:
// 90 - 220
var systolic:uint = (Math.floor((Math.random() *130) + 90));
// 50 - 110 and ~diference of at least 50 //= (Math.floor((Math.random() * 60 )+ 50));
var diastolic:uint
// sets lower number
function setDiastolic(){
var dias:uint = (Math.floor((Math.random() * 60 )+ 50));
if (systolic - dias < 50){
while (systolic - dias < 50){
dias = (Math.floor((Math.random() * 60 )+ 50));
}
}
diastolic = dias;
};
The issue I am having is with the setDiastolic function and the "if" and "while":
With both the "if" and "while" the code works 100% of the time, It just feels redundant and awkward.
When I remove the "if" statement the code works about 85% of the time. However, sometimes the code times out on me.
Like I said, Using both the "if" and "While" works. It just seems like a "patchwork" solution as well as a good learning opportunity for me, so I decided to throw the question out there. Is there a better way to resolve this? Possibly a different approach I could take to accomplish the same thing?
Thanks in advance.
m.
Maybe if you can describe your base logic and what you are trying to accomplish it will be easier to see if there's a better way. As is, I don't see realistic reasoning being applied to relating systolic to diastolic, just tossing out random values and waiting for them to survive the relationship of having a difference >= 50. While your design be considered valid if s=220 and d= 50, I'd be hoping for an ambulance to arrive very soon or BP meter repairman.
Too often when one uses a while loop they take the chance of running a seemingly infinite loop. In your case where there are random values being relied on, there is every possibility of running into a scenario where it is more difficult to find a solution due to the randomness.
Thanks for the response Ned.
I normally avoid while loops for the reasons you stated. In this case it seemed like the best solution for the task.
Right now im just trying to get the backend logic up and running. As to the exact values I am using, They were what was provided to me for this excercise. I realize that 220/50 is a "bad day", and I expect these values to change once more of the project is flushed out and the client has had time to evaluate it.
So here is basically the big picture:
Overall functionality -
The user presses a button, the the BP gauge will randomly animate up to a number between 90-220, then will start to fall. During the fall, there will be a heartbeat sound between 3-5 times at a regular interval with a "twitch" or "jerk" in to the animated needle as the needle falls, with the "diastolic" value being the last heartbeat before the needle sinks back to 0. (As i said before, these are the base numbers i was given, and I am still waiting to hear back on what an "acceptable" range is). Finally the user will enter in the proper values (+-4) into a couple of text fields and submit.
The final goal is to essentially create an excercise to practice taking someones blood pressure with an unlimited question pool. Otherwise, if i would just build this by individual question with hard values and manually animate the whole process. But in this case the client has specifically asked for a high level of reusability/practice.
The logic I am working on now, is intended to set the top number (systolic), then the bottom number (diastolic) while checking to ensure its within an acceptable range (right now this is less than systolic with at least a difference of 50).
Once that is functioning properly, I was planning to add in the interval for the random beats, then program the animation and add the audio.
Does this help clarify things a bit?
thanks again,
M.
You can probably avoid the while loop if you use the systolic value as part of your random calculation though you probably need to set up conditions based on the systolic value... one of which would be a case where the systolic cannot be less than 100 regardless of the 90-220 range you say it can have... If it is between 90 and 100 chances are that is when your while loop is infinitely looping... failing to find a diastolic value that is both > 50 (the minimum diastolic value allowed) and <50 (the need to be 50 below systolic) at the same time.
Since you get the systolic value first, you should be able to use it to set the allowed range of your diastolic.
var systolic:uint = (Math.floor((Math.random() *130) + 90));
var diaMax = systolic -50; // but limited to 110
That last line is just food for thought.
North America
Europe, Middle East and Africa
Asia Pacific