OddEven Sample TypeScript Code

Code:

class OddEven {
    
    even = "Even";
    odd = "Odd";

    convert(num: number): string {

        if (num % 2 === 0) return this.even;
        if (this.isPrime(num)) return num.toString();

        return this.odd;
    }

    convertRange(start: number, end: number): string {

        var result = "";

        for (let i = start; i <= end; i++) {
            result += this.convert(i) + " ";
        }

        return result.trim();
    }

    convertWithString(num: number): string {

        var numString = num.toString();

        if (StringUtilities.endsWith(numString, "0") ||
            StringUtilities.endsWith(numString, "2") ||
            StringUtilities.endsWith(numString, "4") ||
            StringUtilities.endsWith(numString, "6") ||
            StringUtilities.endsWith(numString, "8"))
            return this.even;

        return this.isPrime(num) ? numString : this.odd;
    }

    private isPrime(num: number): boolean {

        if (num === 1) return false;

        for (let i = 3; i <= Math.sqrt(num); i += 2) {
            if (num % i === 0) return false;
        }

        return true;
    }
}

class StringUtilities {

    static endsWith(source: string, value: string): boolean {

        return source.substring(source.length - value.length, source.length) === value;
    }
}

Test:

describe("OddEven", () => {

    var odd = "Odd";
    var even = "Even";
    var oddeven = new OddEven();

    it("oddeven with 1 should return Odd string", () => {
        expect(oddeven.convert(1)).toBe(odd);
    });

    it("oddeven with 2 should return Even string", () => {
        expect(oddeven.convert(2)).toBe(even);
    });

    it("oddeven with 3 should return 3 string", () => {
        expect(oddeven.convert(3)).toBe("3");
    });

    it("oddeven with 4 should return Even string", () => {
        expect(oddeven.convert(4)).toBe(even);
    });

    it("oddeven with 5 should return 5 string", () => {
        expect(oddeven.convert(5)).toBe("5");
    });

    it("oddeven with 6 should return Even string", () => {
        expect(oddeven.convert(6)).toBe(even);
    });

    it("oddeven with 7 should return 7 string", () => {
        expect(oddeven.convert(7)).toBe("7");
    });

    it("oddeven with 8 should return Even string", () => {
        expect(oddeven.convert(8)).toBe(even);
    });

    it("oddeven with 9 should return Odd string", () => {
        expect(oddeven.convert(9)).toBe(odd);
    });

    it("oddeven with 10 should return Even string", () => {
        expect(oddeven.convert(10)).toBe(even);
    });
});

describe("OddEvenRange", () => {

    var oddeven = new OddEven();

    it("oddeven range with start 1 and end 10 should return a valid string", () => {
        expect(oddeven.convertRange(1, 10)).toBe("Odd Even 3 Even 5 Even 7 Even Odd Even");
    });
});

describe("OddEven with String", () => {

    var odd = "Odd";
    var even = "Even";
    var oddeven = new OddEven();

    it("oddeven with 1 should return Odd string", () => {
        expect(oddeven.convertWithString(1)).toBe(odd);
    });

    it("oddeven with 2 should return Even string", () => {
        expect(oddeven.convertWithString(2)).toBe(even);
    });

    it("oddeven with 3 should return 3 string", () => {
        expect(oddeven.convertWithString(3)).toBe("3");
    });

    it("oddeven with 4 should return Even string", () => {
        expect(oddeven.convertWithString(4)).toBe(even);
    });

    it("oddeven with 5 should return 5 string", () => {
        expect(oddeven.convertWithString(5)).toBe("5");
    });

    it("oddeven with 6 should return Even string", () => {
        expect(oddeven.convertWithString(6)).toBe(even);
    });

    it("oddeven with 7 should return 7 string", () => {
        expect(oddeven.convertWithString(7)).toBe("7");
    });

    it("oddeven with 8 should return Even string", () => {
        expect(oddeven.convertWithString(8)).toBe(even);
    });

    it("oddeven with 9 should return Odd string", () => {
        expect(oddeven.convertWithString(9)).toBe(odd);
    });

    it("oddeven with 10 should return Even string", () => {
        expect(oddeven.convertWithString(10)).toBe(even);
    });
});

describe("StringUtilities", () => {

    it("ends with 3", () => {
        expect(StringUtilities.endsWith("3","3")).toBe(true);
    });
});