In: Computer Science
[Javascript] Create a function that gets the total amount and percentage of covid case data for each Age Group
in the covid cases data set(which contains the property "Age Group" in each case array).
Age Groups: 'younger than 18', 'between 19 to 28 Years', 'between 29 to 38 Years','between 39 to 48 Years','between 49 to 58 Years','between 59 to 68 Years','between 69 to 78 Years','between 79 to 88 Years', and'older than 89'
the function getSummaryOfAges(data) should return an Object with the following structure:
{
'UnknownAge': {
totalAmount: 27,
percentageOfTotal: 0.001
},
'younger than 18': {
totalAmount: 39,
percentageOfTotal: 0.074
},
'between 19 to 28 Years': {
totalAmount: 68
percentageOfTotal: 0.124
},
'between 29 to 38 Years': {
totalAmount: 59,
percentageOfTotal: 0.100
},
...etc
}
For the covid case Objects that don't have an Age Group value(meaning it's null).
Then these case Objects should be treated as the "UnknownAge" Age Group category
For each age category, calculate the total number of covid cases(500 cases), the total for each age group,
and finally the percentage of the total.
Example of case object:
Case = {
"Age Group": "29 to 38 Years"
},
{
"Age Group": "19 to 28 Years"
},
...etc
function getSummeryOfAges(data) {
// initialising summeryObject with 0
var summeryObject = {
"UnknownAge": {
totalAmount: 0,
percentageOfTotal: 0
},
"younger than 18": {
totalAmount: 0,
percentageOfTotal: 0
},
"between 19 to 28 Years": {
totalAmount: 0,
percentageOfTotal: 0
},
"between 29 to 38 Years": {
totalAmount: 0,
percentageOfTotal: 0
},
"between 39 to 48 Years": {
totalAmount: 0,
percentageOfTotal: 0
},
"between 49 to 58 Years'": {
totalAmount: 0,
percentageOfTotal: 0
},
"between 59 to 68 Years": {
totalAmount: 0,
percentageOfTotal: 0
},
"between 69 to 78 Years": {
totalAmount: 0,
percentageOfTotal: 0
},
"between 79 to 88 Years": {
totalAmount: 0,
percentageOfTotal: 0
},
"older than 89": {
totalAmount: 0,
percentageOfTotal: 0
},
};
// holds total number of cases
var totalNumber = data.length;
// looping through data
for (var i = 0; i < data.length; i++) {
// checking for current dataAge Group and
// incrementing its age group total and
// recalculates the percentage
switch (data[i]["Age Group"]) {
case "younger than 18":
summeryObject["younger than 18"]["totalAmount"]++;
summeryObject["younger than 18"]["percentageOfTotal"] = (summeryObject["younger than 18"]["totalAmount"] / totalNumber) * 100;
break;
case "19 to 28 Years":
summeryObject["between 19 to 28 Years"]["totalAmount"]++;
summeryObject["between 19 to 28 Years"]["percentageOfTotal"] = (summeryObject["between 19 to 28 Years"]["totalAmount"] / totalNumber) * 100;
break;
case "29 to 38 Years":
summeryObject["between 29 to 38 Years"]["totalAmount"]++;
summeryObject["between 29 to 38 Years"]["percentageOfTotal"] = (summeryObject["between 29 to 38 Years"]["totalAmount"] / totalNumber) * 100;
break;
case "39 to 48 Years":
summeryObject["between 39 to 48 Years"]["totalAmount"]++;
summeryObject["between 39 to 48 Years"]["percentageOfTotal"] = (summeryObject["between 39 to 48 Years"]["totalAmount"] / totalNumber) * 100;
break;
case "49 to 58 Years":
summeryObject["between 49 to 58 Years"]["totalAmount"]++;
summeryObject["between 49 to 58 Years"]["percentageOfTotal"] = (summeryObject["between 49 to 58 Years"]["totalAmount"] / totalNumber) * 100;
break;
case "59 to 68 Years":
summeryObject["between 59 to 68 Years"]["totalAmount"]++;
summeryObject["between 59 to 68 Years"]["percentageOfTotal"] = (summeryObject["between 59 to 68 Years"]["totalAmount"] / totalNumber) * 100;
break;
case "69 to 78 Years":
summeryObject["between 69 to 78 Years"]["totalAmount"]++;
summeryObject["between 69 to 78 Years"]["percentageOfTotal"] = (summeryObject["between 69 to 78 Years"]["totalAmount"] / totalNumber) * 100;
break;
case "79 to 88 Years":
summeryObject["between 79 to 88 Years"]["totalAmount"]++;
summeryObject["between 79 to 88 Years"]["percentageOfTotal"] = (summeryObject["between 79 to 88 Years"]["totalAmount"] / totalNumber) * 100;
break;
case "older than 89":
summeryObject["older than 89"]["totalAmount"]++;
summeryObject["older than 89"]["percentageOfTotal"] = (summeryObject["older than 89"]["totalAmount"] / totalNumber) * 100;
break;
// if Age Group is null
default:
summeryObject["UnknownAge"]["totalAmount"]++;
summeryObject["UnknownAge"]["percentageOfTotal"] = (summeryObject["UnknownAge"]["totalAmount"] / totalNumber) * 100;
break;
}
}
// returns the final object
return summeryObject;
}
// a test case
var Case = [{
"Age Group": "29 to 38 Years"
}, {
"Age Group": "19 to 28 Years"
}, {
"Age Group": "19 to 28 Years"
}, {
"Age Group": "19 to 28 Years"
}, {
"Age Group": null
}, {
"Age Group": "older than 89"
}, ];
// printing it to console
console.log(getSummeryOfAges(Case));
Code Screenshot:
Output:
PS: If you have any doubts/problems please comment below.Thank You