Fizz Buzz Sample TypeScript Code

Code:

class FizzBuzz {
    
    doFizzBuzz(value: number) : string {

        var result = "";
        if (value <= 0) return result;

        if (value % 3 === 0) result = "fizz";
        if (value % 5 === 0) result += "buzz";

        if (!result) result = value.toString();
        return result;
    }

    doFizzBuzzExtended(value: number): string {

        var result = "";
        if (value <= 0) return result;
        var text = value.toString();

        if (value % 3 === 0 || text.search("3") !== -1) result = "fizz";
        if (value % 5 === 0 || text.search("5") !== -1) result += "buzz";
        
        if (!result) return text;
        return result;
    }
    
}

Test:

describe("FizzBuzz", () => {

    var fizzbuzz = new FizzBuzz();

    it("fizzbuzz with 0 should return empty string", () => {
        expect(fizzbuzz.doFizzBuzz(0)).toBe("");
    });

    it("fizzbuzz with 1 should return 1", () => {
        expect(fizzbuzz.doFizzBuzz(1)).toBe("1");
    });

    it("fizzbuzz with 2 should return 2", () => {
        expect(fizzbuzz.doFizzBuzz(2)).toBe("2");
    });

    it("fizzbuzz with 3 should return fizz", () => {
        expect(fizzbuzz.doFizzBuzz(3)).toBe("fizz");
    });

    it("fizzbuzz with 4 should return 4", () => {
        expect(fizzbuzz.doFizzBuzz(4)).toBe("4");
    });

    it("fizzbuzz with 5 should return buzz", () => {
        expect(fizzbuzz.doFizzBuzz(5)).toBe("buzz");
    });

    it("fizzbuzz with 6 should return fizz", () => {
        expect(fizzbuzz.doFizzBuzz(6)).toBe("fizz");
    });

    it("fizzbuzz with 10 should return buzz", () => {
        expect(fizzbuzz.doFizzBuzz(10)).toBe("buzz");
    });

    it("fizzbuzz with 15 should return fizzbuzz", () => {
        expect(fizzbuzz.doFizzBuzz(15)).toBe("fizzbuzz");
    });
});

describe("FizzBuzz extended", () => {

    var fizzbuzz = new FizzBuzz();

    it("fizzbuzz extended with 0 should return empty string", () => {
        expect(fizzbuzz.doFizzBuzzExtended(0)).toBe("");
    });

    it("fizzbuzz extended with 1 should return 1", () => {
        expect(fizzbuzz.doFizzBuzzExtended(1)).toBe("1");
    });

    it("fizzbuzz extended with 2 should return 2", () => {
        expect(fizzbuzz.doFizzBuzzExtended(2)).toBe("2");
    });

    it("fizzbuzz extended with 3 should return fizz", () => {
        expect(fizzbuzz.doFizzBuzzExtended(3)).toBe("fizz");
    });

    it("fizzbuzz extended with 4 should return 4", () => {
        expect(fizzbuzz.doFizzBuzzExtended(4)).toBe("4");
    });

    it("fizzbuzz extended with 5 should return buzz", () => {
        expect(fizzbuzz.doFizzBuzzExtended(5)).toBe("buzz");
    });

    it("fizzbuzz extended with 6 should return fizz", () => {
        expect(fizzbuzz.doFizzBuzzExtended(6)).toBe("fizz");
    });

    it("fizzbuzz extended with 10 should return buzz", () => {
        expect(fizzbuzz.doFizzBuzzExtended(10)).toBe("buzz");
    });

    it("fizzbuzz extended with 13 should return fizz", () => {
        expect(fizzbuzz.doFizzBuzzExtended(13)).toBe("fizz");
    });

    it("fizzbuzz extended with 15 should return fizzbuzz", () => {
        expect(fizzbuzz.doFizzBuzzExtended(15)).toBe("fizzbuzz");
    });

    it("fizzbuzz extended with 25 should return buzz", () => {
        expect(fizzbuzz.doFizzBuzzExtended(25)).toBe("buzz");
    });

    it("fizzbuzz extended with 35 should return fizzbuzz", () => {
        expect(fizzbuzz.doFizzBuzzExtended(35)).toBe("fizzbuzz");
    });

    it("fizzbuzz extended with 52 should return buzz", () => {
        expect(fizzbuzz.doFizzBuzzExtended(52)).toBe("buzz");
    });

    it("fizzbuzz extended with 53 should return fizzbuzz", () => {
        expect(fizzbuzz.doFizzBuzzExtended(53)).toBe("fizzbuzz");
    });
});