Skip to content

A clean, testable implementation of the classic FizzBuzz problem — built in TypeScript using a modular architecture.

License

Notifications You must be signed in to change notification settings

k-angama/fizzbuzz-ts

Repository files navigation

🧮 FizzBuzz Application (TypeScript)

A clean, testable implementation of the classic FizzBuzz problem — built in TypeScript using a modular architecture.


Overview

This application takes a range of numbers (min to max) and applies FizzBuzz rules:

  • Numbers divisible by 3 → "Fizz"
  • Numbers divisible by 5 → "Buzz"
  • Numbers divisible by both 3 and 5 → "FizzBuzz"
  • Other numbers → the number itself

🚀 Features

  • Extensible: Easy to add new rules by implementing the FizzBuzzStrategy interface
  • Priority System: Rules are sorted by priority to ensure correct evaluation order
  • Input Validation: Validates user input for numbers and ranges
  • Type-Safe: Full TypeScript implementation with proper typing
  • Testable: Clean code makes unit testing straightforward
  • Error Handling: Comprehensive error handling with user-friendly messages

🧠 How It Works

  1. The InputHandler asks the user for a minimum and maximum number.
  2. The FizzBuzzApplication validates the input and generates results.
  3. The FizzBuzzFormatter applies a set of rules:
    • FizzRule: multiples of 3 → "Fizz"
    • BuzzRule: multiples of 5 → "Buzz"
    • FizzBuzzRule: multiples of both 3 and 5 → "FizzBuzz"
  4. The formatted result is printed in the console.

⚙️ Installation

Clone the repository and install dependencies:

git clone https://github.com/k-angama/fizzbuzz-ts.git
cd fizzbuzz-ts
npm install

▶️ Run the Application

Build and execute the CLI app:

npm start

You’ll be prompted to enter a minimum and maximum number.

Example:

Enter minimum number: 1
Enter maximum number: 15

=== ✅ FizzBuzz Result ===

1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz

=== ✅ End ===

🧪 Running Tests

This project uses Jest for unit and integration testing.

Run the test suite:

npm test

Typical test coverage includes:

  • FizzBuzzStrategy rules
  • FizzBuzzFormatter logic
  • FizzBuzzApplication flow and error handling

🧩 Project Structure Details

Project Structure

src/
├── core/
│   ├── application/
│   │   ├── FizzBuzzApplication.ts
│   │   └── InputHandler.ts
│   ├── rules/
│   │   ├── FizzBuzzFormatter.ts
│   │   └── FizzBuzzStrategy.ts
│   └── fizzbuzz.ts
├── main.ts
__tests__/
└── (unit tests)

Core Components

FizzBuzzStrategy: Interface defining the contract for all rules

  • priority: Determines evaluation order (higher = first)
  • rule(): Boolean condition for when the rule applies
  • value: String to return when the rule matches

FizzBuzzFormatter: Manages and applies rules

  • Sorts rules by priority
  • Finds the first matching rule for each number
  • Returns formatted output string

FizzBuzzApplication: Application controller

  • Handles user input flow
  • Validates input
  • Returns structured result object

InputHandler: Abstracts user input

  • Interface allows for easy mocking in tests
  • Implementation uses readline

🧑‍💻 Author

k.angama
GitHubLinkedIn


🪪 License

This project is released under the MIT License.

About

A clean, testable implementation of the classic FizzBuzz problem — built in TypeScript using a modular architecture.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published