Question: IN JAVASCRIPT PLEASE Scenario: You are helping prepare some raw data files for analysis by a research team. The team would like to examine the
IN JAVASCRIPT PLEASE
Scenario: You are helping prepare some raw data files for analysis by a research team. The team would like to examine the relationships between weather and crime rates. Specifically, they are looking at crime and weather data from Atlanta in 2021 to see if they can identify relationships between warm or rainy days and the kinds of crimes that are committed. Before this data can be examined statistically, it must be processed to aggregate the crime statistics and output them along with weather data in a single JSON file.
Data:
COBRA-2021.csv
offense_id,rpt_date,occur_date,occur_day,occur_day_num,occur_time,poss_date,poss_time,beat,zone,location,ibr_code,UC2_Literal,neighborhood,npu,lat,long 021140096,5/20/2021,5/19/2021,Wednesday,4,15:20,5/19/2021,15:25,103,1,"1720 MARIETTA BLVD NW ATLANTA, GA 30318 UNITED STATES",23F,LARCENY-FROM VEHICLE,Hills Park,D,33.801693,-84.436453 021791668,6/28/2021,6/27/2021,Sunday,1,21:17,6/28/2021,20:00,102,1,"870 MAYSON TURNER RD NW ATLANTA, GA 30314
Atlanta-weather-2021.csv
time,temperature_2m_max,temperature_2m_min,precipitation_sum,rain_sum,snowfall_sum,precipitation_hours 2021-01-01,66.1,55.4,0.810,0.810,0.00,15.0 2021-01-02,62.6,46.8,0.000,0.000,0.00,0.0 2021-01-03,51.6,37.1,0.000,0.000,0.00,0.0 2021-01-04,54.5,33.3,0.000,0.000,0.00,0.0
App requirements:
App should have a readme.md file that describes what the app is and how you can run it.
Must accept the two incoming and one outgoing file names as command-line arguments
If the user does not provide these three file arguments the app should terminate and display an error on the console.
Here's an example of how to call the app with file arguments. In my solution I put the crime file first, then the weather, then the output name.
(Please put a mention of how to call your app in your readme file):
node app.js crime-data.csv weather-data.csv output-file.json
App should read the supplied crime and weather CSV files into its memory
For the crime data:
There is one record for every crime committed in 2021.
you should pull out the "UC2_Literal" and "occur_date" values from the file
Note that it's possible for no crime to be reported on a certain day
For the weather data:
There is one record for every day of the year in 2021
you should pull out the "time", "temperature_2m_max" and "precipitation_sum" values
Note that even though this is called "time" in the raw weather data, it's actually a date field
Your app should compute the total number of the following crimes that occur on each date (based on the UC2_Literal value).
You should ignore data about crimes that occurred outside of the 365 days that we care about in 2021. Similarly, ignore crimes with invalid or unknown occur_date values.
These values will make their way into the output file described below:
totalCrimes: total number of crimes for this date
autoCrimes: crimes with a "LARCENY-FROM VEHICLE" or "AUTO THEFT" value
violentCrimes: crimes with an "AGG ASSAULT" or "HOMICIDE" value
otherCrimes: total number of crimes that aren't auto or violence related (in other words, all remaining crimes)
App should compute the following for the weather data.These values will make their way into the output file described below:
date: the date the weather report is for. Will be in the format "YYYY-MM-DD".
rainyDay: will be true if the precipitation_sum is greater than 0.1 (measured in inches)
hotDay: will be true if temperature_2m_max is greater than or equal to 85 (measured in Fahrenheit)
coldDay: will be true if temperature_2m_max is less than or equal to 40 (measured in Fahrenheit)
niceDay: will be true if rainyDay, hotDay, and coldDay are all false
App will output a JSON file containing these aggregate values for each day in 2021
The JSON file will contain an array of objects that contain all the variable values from both the
Here's an example of an output record for a day that does not have any crime data:
{"date":"2021-01-01","rainyDay":true,"hotDay":false,"coldDay":false,"niceDay":false,"totalCrimes":0,"autoCrimes":0,"violentCrimes":0,"otherCrimes":0} Here's an example of an output record for a day that does have crimes reported:
{"date":"2021-01-16","rainyDay":false,"hotDay":false,"coldDay":false,"niceDay":true,"totalCrimes":61,"autoCrimes":33,"violentCrimes":8,"otherCrimes":20} The app should have appropriate error-checking code, including:
try/catch statements around file operations (or an equivalent onError event handler).
Checks for if a line in the file does not contain a proper date value.
Hints and suggestions:
Take an incremental approach to solving this problem. Start by reading the data from the CSV files and outputting it using the console.table() Links to an external site.function (which is really neat, btw.)
Once you have read the data properly, process the contents of each line of data.
In the case of the crime data, you will want to compile all the crimes that happened on a specific day. There are several ways to do this, including using a reduce() Links to an external site.function. I also recommend using an object with a key for each date and the value being an object that contains the running totals for each kind of crime.
For the weather data, you might want to consider using a map() Links to an external site.function on the data since there's a one-to-one correspondence between each row coming in and each row going out into the final JSON file
Be careful about the date differences between each file! You will have to convert one file's format into the other in order to correlate the aggregate totals from both data sets.The Day.js Links to an external site.library is useful for this. For example, you can do something like this to convert one date string to another:
const formattedDate = dayjs(day.date).format("M/DD/YYYY"); If you want to consolidate the contents of two different objects, you can use the spread operator
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
