The Code
Click the headers below to hide/show the corresponding code excerpts.
This first section is where global constants and variables are declared and/or initialized. Only one constant is needed in this app, and it's an array of objects representing the default entries in the table of events. These events will be the ones present in the table the first time a device accesses the app, and they will be the only ones present when the device's local storage is reset.
This function is called when the app is loaded, and every time the user updates the table of events (by adding rows).
It populates the dropdown used to filter by city, with all the unique city names present in the recorded events. It does so by constructing a Set from an array of each event's city name, mapped from the array of the recorded event objects. Then the dropdown is cleared of its contents, and refilled with options made from the names in the Set. Lastly, the event table and summary section are refreshed to reflect all recorded events.
This function returns all of the events recorded in local storage. It is used when building the dropdown, filtering the table, and/or saving a new event.
If the app has no data recorded in local storage, the array of default event objects is recorded and then returned. Otherwise, the array of recorded events is pulled from local storage and returned.
The default array is converted into a string in JavaScript Object Notation (JSON) format before storage. When pulled, the recorded JSON data is parsed and converted back into an array of event objects.
This function is used to save an array of event objects in local storage. It is called in two situations: first, when the user creates a new event to add to the collection of recorded ones; and second, to save the default events to local storage after an attempt to pull from local storage produces no data.
The array is converted into a string in JavaScript Object Notation (JSON) format before being saved into local storage. When saved, this string will replace the preexisting one (if present).
This function is used to populate the events table with a passed-in array of event objects. It is called when building the city-filtering dropdown, and also when filtering the table by city.
The function starts with emptying the table of all its contents. Then it iterates through the elements of the passed-in array, creates a new row element for each one, and inserts them into the events table.
This function is used to calculate four stats - total, average, maximum, and minimum attendance counts - for a passed-in array of event objects. It is called when updating the stats summary section of the app based on this array.
All four stats are contained in a single object that gets returned in the end. To calculate these stats, the array is iterated through once, and each stat is updated at every point in the loop. The one exception is the average, which is calculated *after* iterating through the array; this minimizes the number of divisions needed.
This function is used update the stats summary section according to a passed-in array of event objects. It is called when building the city-filtering dropdown, and when filtering the event table by city.
When called, this function first calls calculateStats
to calculate the total, average, minimum, and maximum attendance
counts for the passed-in array. Then these values are inserted into
their respective parts of the summary section.
This function is used to filter the events table by city. It is called any time the user selects a new option in the city-filtering dropdown.
When called, this function retrieves the array of recorded event
objects. From that array, a new one is created via the
filter
array method. This new array contains only the
event objects whose city name matches the passed-in value. The
events table and stats summary section are then updated according
to this new array.
This function is used to save a new user-inputted event to local storage with all the other recorded events. It is called any time the user appropriately fills the form on the "Add A New Event" modal and clicks the Save button.
The form entries are used to create a new event object. Then the
array of existing recorded event objects is retrieved, and this new
object is added onto the end of it. This new array is then saved to
local storage. Finally, the modal is cleared and hidden, and - via
buildDropdown
- updates are made to the city-filtering
dropdown, events table, and stats summary section to include the
newly-added event.