# Noob question re Simple Calculations

Just starting with drafts. Was wondering if it’s possible to capture variables and do simple calculations within drafts?

What I would like to be able to do is enter a weight and then dosing of up to 3-4 medications, and get back a value for dosing mg/kg… so for example weight = 10 kg, taking med#1 at 100mg in AM and 100mg in PM (total 200mg), so (200/10) = 20 mg/kg and so on…

Yes.

Drafts uses JavaScript for scripting steps within actions and so has access to variables and can perform mathematical operations using that.

Drafts can use a prompt (or a fancier but more complex HTML prompt) to capture user information to populate the variables.

The captured data and calculation results can then be displayed, added as content into a new or existing draft, written to a file, or even sent via an API to a compatible web service.

On the Mac, you also have the option to call shell scripts instead of using JavaScript which opens up even more possibilities of you have a greater familiarity with other languages that could be accessed/executed from the terminal.

Is that enough to get you started?

Thanks! First time in javascript, it’s ugly but I think I was able to get it to work. Would welcome your suggestions

``````let f = () => {
let p = Prompt.create();
p.title = "Outside Call";
p.message = "Record your ouside call below.";

p.addSelect("rshp", "Relationship to Patient", ["Pediatrician", "ED Doc", "GP", "Parent"], ["Pediatrician"], false);

if (p.show()) {
d = Draft.create();
d.content = `# Outside Call

`;
}

if (p.buttonPressed == "create") {
let caller = p.fieldValues["Caller"];
let phone = p.fieldValues["Phone"];
let rshp = p.fieldValues["rshp"];

let pt = p.fieldValues["Patient"];
let dob = p.fieldValues["DOB"];
let mrn = p.fieldValues["MRN"];
let phn = p.fieldValues["PHN"];
let wt = p.fieldValues["Weight"];

let med1 = p.fieldValues["med1"]
let dose1am = p.fieldValues["dose1am"];
let dose1pm = p.fieldValues["dose1pm"];
let dose1hs = p.fieldValues["dose1hs"];
let total1 = ((+(dose1am)) + (+(dose1pm)) + (+(dose1hs)))

let med2 = p.fieldValues["med2"];			let dose2am = p.fieldValues["dose2am"];
let dose2pm = p.fieldValues["dose2pm"];
let dose2hs = p.fieldValues["dose2hs"];
let total2 = ((+(dose2am)) + (+(dose2pm)) + (+(dose2hs)))

let med3 = p.fieldValues["med3"]
let dose3am = p.fieldValues["dose3am"];
let dose3pm = p.fieldValues["dose3pm"];
let dose3hs = p.fieldValues["dose3hs"];
let total3 = ((+(dose3am)) + (+(dose3pm)) + (+(dose3hs)))
let med4 = p.fieldValues["med4"]
let dose4am = p.fieldValues["dose4am"];
let dose4pm = p.fieldValues["dose4pm"];
let dose4hs = p.fieldValues["dose4hs"];
let total4 = ((+(dose4am)) + (+(dose4pm)) + (+(dose4hs)))

let mgkg1 = total1 / wt
let mgkg2 = total2 / wt
let mgkg3 = total3 / wt
let mgkg4 = total4 / wt

let prob = p.fieldValues["prob"];
let plan = p.fieldValues["plan"];

if (prob.length == 0) {
prob = "[none]";
}
let entry = `Outside Call
\${strftime(new Date(), "%B %d, %Y %H:%M%p")}
\n
# Caller Info:
Name: \${caller}
Phone:  \${phone}
Relationship:  \${rshp}

# Patient Details:
Name:  \${pt}
DOB:  \${dob}
MRN:  \${mrn}
PHN:  \${phn}
Weight:  \${wt} kg

# Current Medications
1. \${med1} \${dose1am} \${dose1pm} \${dose1hs} (\${mgkg1} mg/kg)
2. \${med2} \${dose2am} \${dose2pm} \${dose2hs} (\${mgkg2} mg/kg)
3. \${med3} \${dose3am} \${dose3pm} \${dose3hs} (\${mgkg3} mg/kg)
4. \${med4} \${dose4am} \${dose4pm} \${dose4hs} (\${mgkg4} mg/kg)

# Problem:
\${prob}

# Plan:
\${plan}`;
d.content = entry;
d.update();