Skip to content

malikinss/telran-backend-62-hw3

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Homework 3 – Titanic Statistics Analysis

Task Definition

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:

  1. Calculating total fares paid by all passengers.
  2. Calculating average fare for each passenger class (1st, 2nd, and 3rd).
  3. Calculating total number of survived and non-survived passengers.
  4. 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.


📝 Description

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

🎯 Purpose

The homework focuses on practicing:

  1. File handling in Node.js – reading external files using fs/promises.
  2. CSV parsing – safely splitting CSV rows including quoted values.
  3. Data analysis – calculating statistical metrics from real-world data.
  4. Object-oriented design – organizing dataset logic inside a reusable class.
  5. Modern JavaScript features – private class fields, async/await, ES modules.

🔍 How It Works

1. Reading the CSV File

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.

2. Parsing the Dataset

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"
}

3. Data Analysis

The Titanic class performs all statistical calculations:

Total Fares

getTotalFares();

Sums all values in the Fare column.


Average Fare by Class

getAvgFaresByClass();

Calculates average ticket price for each passenger class.

Example output:

{
  "1": 84.15,
  "2": 20.66,
  "3": 13.68
}

Total Survived Passengers

getTotalSurvived();

Counts rows where:

Survived === "1"

Survived by Gender

getTotalSurvivedByGender();

Groups survived passengers by Sex.

Example output:

{
  male: 109,
  female: 233
}

Survived Children

getTotalSurvivedChildren((ageLimit = 18));

Counts survived passengers younger than the specified age.


📜 Output Example

Running the application:

node index.js

Example 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

📦 Usage

Run the analysis script:

node index.js

The program will:

  1. Load train.csv
  2. Parse the dataset
  3. Calculate statistics
  4. Print the results in the console

✅ Dependencies

  • Node.js 18+
  • Built-in Node.js modules:
    • fs/promises

No external libraries are required.


📊 Project Status

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.

📄 License

MIT License


🧮 Conclusion

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 🎓

About

A Node.js data analysis project that parses the Titanic CSV dataset and computes statistics such as total fares, average fares by passenger class, and survival metrics by gender and age.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors