Skip to content
5 changes: 2 additions & 3 deletions Sprint-3/2-practice-tdd/get-ordinal-number.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
function getOrdinalNumber(num) {

if (typeof num !== "number" || !Number.isInteger(num) || num <= 0) {
if (!Number.isInteger(num) || num <= 0) {
return "Invalid input";
}
if (num % 100 >= 11 && num % 100 <= 13) {
if (num % 100 >= 11 && num % 100 <= 13) {
return num + "th";
}
switch (num % 10) {
Expand Down
67 changes: 28 additions & 39 deletions Sprint-3/2-practice-tdd/get-ordinal-number.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,70 +8,59 @@ const getOrdinalNumber = require("./get-ordinal-number");
// When the number is 1,
// Then the function should return "1st"

test("should return '1st' for 1", () => {
// category 1: ends with 1 (but not 11) -> "st"
test("append 'st' to numbers ending in 1, except those ending in 11", () => {
expect(getOrdinalNumber(1)).toEqual("1st");
expect(getOrdinalNumber(21)).toEqual("21st");
expect(getOrdinalNumber(101)).toEqual("101st");
});

// Case 2: Identify the ordinal number for 2
// When the number is 2,
// Then the function should return "2nd"

test("should return '2nd' for 2", () => {
// category 2: ends with 2 (but not 12) -> "nd"
test("append 'nd' to numbers ending in 2, except those ending in 12", () => {
expect(getOrdinalNumber(2)).toEqual("2nd");
expect(getOrdinalNumber(22)).toEqual("22nd");
expect(getOrdinalNumber(102)).toEqual("102nd");
});

// Case 3: Identify the ordinal number for 3
// When the number is 3,
// Then the function should return "3rd"

test("should return '3rd' for 3", () => {
// category 3: ends with 3 (but not 13) -> "rd"
test("append 'rd' to numbers ending in 3, except those ending in 13", () => {
expect(getOrdinalNumber(3)).toEqual("3rd");
expect(getOrdinalNumber(23)).toEqual("23rd");
expect(getOrdinalNumber(103)).toEqual("103rd");
});

// Case 4: Identify the ordinal number for 4
// When the number is 4,
// Then the function should return "4th"

test("should return '4th' for 4", () => {
// category 4: all other numbers -> "th"
test("append 'th' to all other numbers", () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When this test fails, the description "all other numbers" is not quite informative; one would need to read the other tests to figure out what numbers are considered as "other numbers".

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed to more detailed description, "append 'th' to numbers ending in 0, 4–9, or 11–13"

expect(getOrdinalNumber(4)).toEqual("4th");
});

// Case 5: Identify the ordinal number for 11
// When the number is 11,
// Then the function should return "11th"

test("should return '11th' for 11", () => {
expect(getOrdinalNumber(11)).toEqual("11th");
expect(getOrdinalNumber(12)).toEqual("12th");
expect(getOrdinalNumber(13)).toEqual("13th");
expect(getOrdinalNumber(14)).toEqual("14th");
expect(getOrdinalNumber(111)).toEqual("111th");
expect(getOrdinalNumber(112)).toEqual("112th");
expect(getOrdinalNumber(113)).toEqual("113th");
Comment on lines +49 to +54
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why test the same numbers again in a separate test?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right all the test under category 5: special case for 11, 12, 13 -> "th" can be removed since they are tested in category 4: all other numbers -> "th"

expect(getOrdinalNumber(114)).toEqual("114th");
});

// Case 6: Identify the ordinal number for 21
// When the number is 21,
// Then the function should return "21st"

test("should return '21st' for 21", () => {
expect(getOrdinalNumber(21)).toEqual("21st");
});

// Case 7: Identify the ordinal number for 102
// When the number is 102,
// Then the function should return "102nd"

test("should return '102nd' for 102", () => {
expect(getOrdinalNumber(102)).toEqual("102nd");
});

// Case 8: Identify the ordinal number for 111
// When the number is 111,
// Then the function should return "111th"

test("should return '111th' for 111", () => {
// category 5: special case for 11, 12, 13 -> "th"
test("append 'th' to numbers ending in 11, 12, or 13", () => {
expect(getOrdinalNumber(11)).toEqual("11th");
expect(getOrdinalNumber(12)).toEqual("12th");
expect(getOrdinalNumber(13)).toEqual("13th");
expect(getOrdinalNumber(111)).toEqual("111th");
expect(getOrdinalNumber(112)).toEqual("112th");
expect(getOrdinalNumber(113)).toEqual("113th");
});

// case 9: identify the ordinal number for 211
// when the number is 211
// then the function should return "211th"

test("should return '33rd' for 33", () => {
expect(getOrdinalNumber(33)).toEqual("33rd");
});
7 changes: 3 additions & 4 deletions Sprint-3/2-practice-tdd/repeat.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
function repeat() {
function repeat(str, count) {
if (arguments.length !== 2) {
throw new Error("Function requires exactly two arguments: str and count.");
}

const [str, count] = arguments;

if (typeof str !== "string") {
throw new Error("First argument must be a string.");
}

if (typeof count !== "number" || !Number.isInteger(count) || count < 0) {
if (!Number.isSafeInteger(count) || count < 0) {
throw new Error("Second argument must be a non-negative integer.");
}

Expand All @@ -28,4 +26,5 @@ function repeat() {
return result;
}


module.exports = repeat;