The goal of Homework 3 is to analyze passenger data from the Titanic dataset (train.csv) and compute several statistical metrics.
Specifically, the task requires:
- Calculating total fares paid by all passengers.
- Calculating average fare for each passenger class (1st, 2nd, and 3rd).
- Calculating total number of survived and non-survived passengers.
- Calculating total number of survived men, women, and children (under 18 years old).
The dataset is provided in CSV format, therefore the project must include logic for reading and parsing CSV files.
This project implements a Titanic dataset analysis tool in Node.js that reads passenger data from a CSV file and calculates key statistics related to fares, survival rates, and demographics.
The project consists of:
- CSV parsing utilities – handle file reading and CSV parsing while respecting quoted values.
- Titanic analysis class – encapsulates all dataset processing logic.
- Main script (
index.js) – loads the dataset and prints statistics to the console.
Project structure:
.
├── index.js # Entry point of the application
├── train.csv # Titanic dataset
└── src
├── Titanic.js # Dataset analysis class
└── file_handling.js # CSV reading and parsing utilities
The homework focuses on practicing:
- File handling in Node.js – reading external files using
fs/promises. - CSV parsing – safely splitting CSV rows including quoted values.
- Data analysis – calculating statistical metrics from real-world data.
- Object-oriented design – organizing dataset logic inside a reusable class.
- Modern JavaScript features – private class fields, async/await, ES modules.
The module file_handling.js reads the dataset using:
fs.readFile(filename, "utf-8");The text is then:
- Split into lines
- Cleaned from empty rows
- Parsed using a CSV-safe regex to correctly handle quoted commas.
parseCSV() converts the CSV text into an array of objects, where:
Header → Object key
Row value → Object value
Example:
{
PassengerId: "1",
Survived: "0",
Pclass: "3",
Name: "Braund, Mr. Owen Harris",
Sex: "male",
Age: "22",
Fare: "7.25"
}The Titanic class performs all statistical calculations:
getTotalFares();Sums all values in the Fare column.
getAvgFaresByClass();Calculates average ticket price for each passenger class.
Example output:
{
"1": 84.15,
"2": 20.66,
"3": 13.68
}
getTotalSurvived();Counts rows where:
Survived === "1"
getTotalSurvivedByGender();Groups survived passengers by Sex.
Example output:
{
male: 109,
female: 233
}
getTotalSurvivedChildren((ageLimit = 18));Counts survived passengers younger than the specified age.
Running the application:
node index.jsExample output:
=== Titanic Dataset Stats ===
Total Fares: 28693.95
Average Fares by Class: { '1': 84.15, '2': 20.66, '3': 13.68 }
Total Survived: 342
Total Survived by Gender: { male: 109, female: 233 }
Total Survived Children (under 18): 61
Run the analysis script:
node index.jsThe program will:
- Load
train.csv - Parse the dataset
- Calculate statistics
- Print the results in the console
- Node.js 18+
- Built-in Node.js modules:
fs/promises
No external libraries are required.
Status: ✅ Completed
- CSV parsing implemented with quoted value support.
- Dataset converted into structured JavaScript objects.
- Titanic analysis class provides multiple statistical methods.
- Asynchronous file handling implemented with
async/await.
MIT License
This project demonstrates how to build a data analysis utility in Node.js using:
- Asynchronous file handling
- CSV parsing techniques
- Object-oriented design
- Real-world dataset processing
The implementation keeps data parsing and analysis logic separated, making the code more modular, reusable, and easier to extend.
Made with ❤️ and JavaScript by Sam Malikin 🎓