have an array that I need to the following with:
- arranged from lowest to highest.
- Select the middle two numbers.
- Sum the middle two scores.
Example:
var numArray = [2, 1, 3, 1, 4, 2];
numArray.sort((a, b) => a - b);
var sum = numArray[2] + numArray[3];
var sum = numArray[2] + numArray[3]; gives me two numbers together and not their sum in Drafts but Online javascript editors produce the sum of 4.
I had to change the code to this for Drafts to process numbers.
var sum = numArray[2] + numArray[3]
Why is there a difference between online editors that process numbers and Drafts that processes strings?
Full Code that process strings instead of numbers:
var p = Prompt.create();
p.title = "Psychiatric Impairment Rating Scale (PIRS) Impairment";
p.message = '1 - no deficit, 2 - mild impairment; 3 - moderate impairment; 4 - Severe impairment; 5 - totally impaired'
p.isCancellable = true;
p.addButton('OK');
p.addTextField('activitiesOfDailyLiving', 'Self-Care, Personal Hygiene and Activities of Daily Living', '');
p.addTextField('socialRecreational', 'Social and Recreational', '');
p.addTextField('travel', 'Travel', '');
p.addTextField('interpersonalRelationships', 'Interpersonal Relationships', '');
p.addTextField('concentrationPersistencePace', 'Concentration, Persistence and Pace', '');
p.addTextField('resilienceEmployability', 'Resilience and Employability', '');
if (p.show()) {
var activitiesOfDailyLiving = p.fieldValues["activitiesOfDailyLiving"];
var socialRecreational = p.fieldValues["socialRecreational"];
var travel = p.fieldValues["travel"];
var interpersonalRelationships = p.fieldValues["interpersonalRelationships"];
var concentrationPersistencePace = p.fieldValues["concentrationPersistencePace"];
var resilienceEmployability = p.fieldValues["resilienceEmployability"];
var numArray = [activitiesOfDailyLiving, socialRecreational, travel, interpersonalRelationships, concentrationPersistencePace,resilienceEmployability];
alert(numArray);
// 1. Arranged from lowest to highest.
numArray.sort((a, b) => a - b);
// 2. Select the middle two numbers.
//var middleOne = Number(numArray[2]);
//var middleTwo = numArray[3];
//3. Sum the middle two scores.
//(numArray[2] and (numArray[3] are actually strings, not numbers. The easiest way to produce a number from a string is to prepend it with `+`. Alternatively use Number(numArray[2]) + Number(numArray[3])
var sum = numArray[2] + numArray[3];
alert(sum);
const impairmentScore = {2:0, 3:5, 4:10, 5:15, 6:20, 7:30, 8:40, 9:50, 10:50};
var scorePIRS = impairmentScore[sum];
alert(scorePIRS);
var textPIRS = `The Psychiatric Impairment Rating Scale (PIRS) captures behavioural/functional consequences of mental disorders on 6 subscales that each measure a domain of functional impairment on a 5-point scale. The six domains measured include Self-care, Personal Hygiene, and Activities of Daily Living Role Functioning, Social and Recreational Activities Travel Interpersonal Relationships Concentration, Persistence, and Pace Resilience and Employability. Ms. XX's score places him the Impairment Score of ${scorePIRS}%.`
editor.setSelectedText (textPIRS);
}