In: Computer Science
Write a function that takes an Array of cases, as well as an FSA String value.
This Case data is an Array where each item in the Array is a
custom Case Object. The Array is really [case1, case2, case3, ...],
and each Case Object has the following structure:
{
"Age Group": "40 to 49 Years",
"Neighbourhood Name": "Annex",
"Outcome": "RESOLVED",
"Client Gender": "FEMALE",
"Classification": "CONFIRMED",
"FSA": "M5R",
"Currently Hospitalized": "No",
"Episode Date": "2020-09-12",
"Assigned_ID": 17712,
"Outbreak Associated":
"Sporadic",
"Ever Intubated": "No",
"Reported Date": "2020-09-16",
"Currently in ICU": "No",
"Source of Infection":
"Healthcare",
"_id": 161028,
"Ever in ICU": "No",
"Ever Hospitalized": "No",
"Currently Intubated":
"No"
}
An FSA if a forward sortation area (the first 3 characters in a
Canadian postal code):
https://en.wikipedia.org/wiki/Postal_codes_in_Canada#Forward_sortation_areas
Return a new Array with only those case Objects that contain the
given fsa value.
You should support the FSA value being CAPITALIZED or lowercase, as
well as deal with any case Objects that don't have an FSA value
(e.g., null/undefined)
In your solution, make use of the following:
- create an empty array
- use a for...of loop to loop over all Objects in cases
- if a case includes the given fsa, add the case Object
to the empty Array
Your function should return the newly created Array.
function getCasesByFSA(cases, fsa) {}
Hello! :)
Here's a documented code to solve the assignment:
/**
* Returns a new Array with only those case Objects that contain the given fsa value.
* @param cases An array of case Objects.
* @param fsa A string representing the FSA(forward sortatin area).
* @returns A newly created array of case Objects that contain the matching fsa value.
*/
function getcasesByFSA(cases, fsa) {
// create an empty array
const result = [];
// use a for...of loop to loop over all Objects in cases
for(eachcase of cases) {
if(
eachcase['FSA'] // if a case includes the given fsa
&& // and
fsa.toUpperCase() === eachcase['FSA'].toUpperCase() // the uppercase FSA value of the case Object and the passed fsa values match
) {
// add the case Object to the empty Array
result.push(eachcase);
}
}
return result;
}
Hope this helps! :)