Question

In: Computer Science

[Javascript] Create a function that gets the total amount and percentage of covid case data for...

[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


Solutions

Expert Solution

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


Related Solutions

Javascript Calculator Algorithm to calculate a tip percentage given the bill amount and total bill including...
Javascript Calculator Algorithm to calculate a tip percentage given the bill amount and total bill including tip. Asker suer for bill without tip: Ask the user for total bill with tip: Ask the user how many people splitting bill: Submit button to calculate the tip percentage
[Javascript] Create a function(returnObjectFromId(case, ...idNum)) to return the case Object(s) for a given idNum, or list...
[Javascript] Create a function(returnObjectFromId(case, ...idNum)) to return the case Object(s) for a given idNum, or list of idNums. Calling with a single `idNum` value should return the case Object, and return NULL if an id value that's unknown is passed returnObjectFromId(case, 84838) would return the Object in the cases Array with an 'idNumber' of id, and use the .find() method of the cases Array to locate items by idNumber. returnObjectFromId(cases, -23298312) would return null. returnObjectFromId(cases, 161020, 161021) would return an...
Create a JavaScript function that will collect the information from the form and verify that it...
Create a JavaScript function that will collect the information from the form and verify that it is the correct type and that there are no blank textboxes. Save and test the file to ensure that the textbox information is collected and the script is working correctly. Use the onclick event within the submit button to call this function. Output validation error messages by writing directly to the with the id of "results." (You may want to use alert boxes for...
If it needs more information be specific. JavaScript Functions Create the makeBranches() function that will be...
If it needs more information be specific. JavaScript Functions Create the makeBranches() function that will be used to append node branches to the node tree diagram. The function will have two parameters named treeNode and nestedList. The treeNode parameter stores the current node from the source article and the nestedList parameter stores the structure of the node tree displayed in the web page. Add the commands described in the steps below. Each time the makeBranches() function is called, it is...
Do for javascript. Create a form with the following inputs. L -> Loan Amount in $'s,...
Do for javascript. Create a form with the following inputs. L -> Loan Amount in $'s, i -> Interest Rate %/year, n -> Number of Compounding Periods months Given the following formula to solve for the monthly payments mp = ( i * (1+i)^n * L ) / ( (1+i)^n - 1 ) Output the results L = $, i = %/month , n = months mp = $ And a table which lists the remaining loan amount and interest...
Write a javascript program according to the follow requirements: Create a function that converts Fahrenheit to...
Write a javascript program according to the follow requirements: Create a function that converts Fahrenheit to Celsius. It takes a single argument which represents degrees in Fahrenheit. It converts it and returns the degrees in Celsius. Create another function that converts Celsius to Fahrenheit. It takes a argument in Celsius and returns the degrees in Fahrenheit. Implement the function convert(isFtoC, from, to) below. It takes the following three arguments: isFtoC: a boolean that is true if degrees must be converted...
The code to create a Search/Filter Data with Javascript or html from html page.
The code to create a Search/Filter Data with Javascript or html from html page.
What is the total amount of healthcare expenditures in the US? Provide a specific amount in dollars and percentage with respect to GDP.
  What is the total amount of healthcare expenditures in the US? Provide a specific amount in dollars and percentage with respect to GDP. During the first part of the semester we have spent a great deal of time reviewing general concepts of economics to build the foundation to understand specific issues of the healthcare sector. What general economic assumptions do not apply to healthcare? Explain why. Define the concept of adverse selection and explain why it is important in...
USING JAVASCRIPT Create a file name dayOfWeek.js and write an arrow function named dayOfWeek that accepts...
USING JAVASCRIPT Create a file name dayOfWeek.js and write an arrow function named dayOfWeek that accepts a Date object dateStr and returns a string that is the day of the week in English form (i.e. “Sunday”, “Monday”, etc.). Test your function by creating a date object that is a significant date to you (such as your birthday) and passing that date object to your function. Test your function at least twice with two different dates. Submit the dayOfWeek.js file to...
Using HTML/Javascript (if needed) I want to create a table for a website that pulls data...
Using HTML/Javascript (if needed) I want to create a table for a website that pulls data from another website where the data is being updated on. Example: https://investors.coca-colacompany.com/stock-information/historical-data I cannot just put in the numbers to a table as they will be getting updated daily. So it needs to link the the website elements. DATE OPEN HIGH LOW CLOSE VWAP VOLUME % CHG CHANGE TRADE VAL TOTAL TRADES 2020-10-13 -- -- -- 51.09 -- -- 0.00% -- -- -- 2020-10-12...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT