Skip to content

Commit a57813f

Browse files
committed
Improve variable names and test coverage for getOrdinalNumber
Renamed variables for clarity and added more complete, grouped test cases
1 parent 1413394 commit a57813f

File tree

3 files changed

+38
-41
lines changed

3 files changed

+38
-41
lines changed
Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
function getOrdinalNumber(num) {
2-
const itsTwoLastDigs = num % 100;
3-
if (itsTwoLastDigs === 11 || itsTwoLastDigs === 12 || itsTwoLastDigs === 13) {
2+
const numberLastTwoDigits = num % 100;
3+
4+
if (numberLastTwoDigits === 11 || numberLastTwoDigits === 12 || numberLastTwoDigits === 13) {
45
return num + "th";
56
}
67

7-
const lastDig = num % 10;
8-
if (lastDig === 1) return num + "st";
9-
if (lastDig === 2) return num + "nd";
10-
if (lastDig === 3) return num + "rd";
8+
const numberLastDigit = num % 10;
9+
if (numberLastDigit === 1) return num + "st";
10+
if (numberLastDigit === 2) return num + "nd";
11+
if (numberLastDigit === 3) return num + "rd";
1112
return num + "th";
1213
}
1314

14-
15-
16-
1715
module.exports = getOrdinalNumber;
Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,42 @@
11
const getOrdinalNumber = require("./get-ordinal-number");
2-
// In this week's prep, we started implementing getOrdinalNumber
3-
4-
// continue testing and implementing getOrdinalNumber for additional cases
5-
// Write your tests using Jest - remember to run your tests often for continual feedback
6-
7-
// Case 1: Identify the ordinal number for 1
8-
// When the number is 1,
9-
// Then the function should return "1st"
102

113
describe("getOrdinalNumber", () => {
12-
test("append 'st' to numbers ending in 1 expect 11", () => {
13-
expect(getOrdinalNumber(1)).toEqual("1st");
14-
expect(getOrdinalNumber(101)).toEqual("101st");
15-
});
16-
test("should return '11th' for 11", () => {
17-
expect(getOrdinalNumber(11)).toEqual("11th");
4+
test("returns 'st', 'nd', 'rd', 'th' for standard numbers", () => {
5+
expect(getOrdinalNumber(1)).toBe("1st");
6+
expect(getOrdinalNumber(2)).toBe("2nd");
7+
expect(getOrdinalNumber(3)).toBe("3rd");
8+
expect(getOrdinalNumber(4)).toBe("4th");
9+
expect(getOrdinalNumber(10)).toBe("10th");
1810
});
1911

20-
test("append 'rd' to numbers ending in 3", () => {
21-
expect(getOrdinalNumber(3)).toEqual("3rd");
22-
expect(getOrdinalNumber(23)).toEqual("23rd");
23-
expect(getOrdinalNumber(33)).toEqual("33rd");
24-
expect(getOrdinalNumber(103)).toEqual("103rd");
12+
test("handles numbers ending with 11, 12, 13 as special cases", () => {
13+
expect(getOrdinalNumber(11)).toBe("11th");
14+
expect(getOrdinalNumber(12)).toBe("12th");
15+
expect(getOrdinalNumber(13)).toBe("13th");
16+
expect(getOrdinalNumber(111)).toBe("111th");
17+
expect(getOrdinalNumber(112)).toBe("112th");
18+
expect(getOrdinalNumber(113)).toBe("113th");
2519
});
26-
test("should return '4th' for 4", () => {
27-
expect(getOrdinalNumber(4)).toEqual("4th");
20+
21+
test("handles numbers ending with 1, 2, or 3 correctly beyond 100", () => {
22+
expect(getOrdinalNumber(101)).toBe("101st");
23+
expect(getOrdinalNumber(102)).toBe("102nd");
24+
expect(getOrdinalNumber(103)).toBe("103rd");
25+
expect(getOrdinalNumber(104)).toBe("104th");
26+
expect(getOrdinalNumber(132)).toBe("132nd");
2827
});
2928

30-
test("should return '12th' for 12", () => {
31-
expect(getOrdinalNumber(12)).toEqual("12th");
29+
test("returns '0th' for zero", () => {
30+
expect(getOrdinalNumber(0)).toBe("0th");
3231
});
33-
test("should return '13th' for 13", () => {
34-
expect(getOrdinalNumber(13)).toEqual("13th");
32+
33+
test("works with large numbers", () => {
34+
expect(getOrdinalNumber(1001)).toBe("1001st");
35+
expect(getOrdinalNumber(1012)).toBe("1012th");
3536
});
3637

37-
test("append 'nd' to numbers ending in 2, except those ending in 12", () => {
38-
expect(getOrdinalNumber(2)).toEqual("2nd");
39-
expect(getOrdinalNumber(22)).toEqual("22nd");
40-
expect(getOrdinalNumber(132)).toEqual("132nd");
38+
test("handles negative numbers", () => {
39+
expect(getOrdinalNumber(-1)).toBe("-1st");
40+
expect(getOrdinalNumber(-11)).toBe("-11th");
4141
});
4242
});

Sprint-3/2-practice-tdd/repeat-str.test.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,8 @@ test("should repeat the string count times", () => {
4242
// Given a target string str and a negative integer count,
4343
// When the repeatStr function is called with these inputs,
4444
// Then it should throw an error or return an appropriate error message, as negative counts are not valid.
45-
test("should repeat the string count times", () => {
45+
test("should throw an error when count is negative", () => {
4646
const str = "hello";
4747
const count = -1;
48-
const repeatedStr = repeat(str, count);
49-
expect(repeatedStr).toEqual("invalid");
48+
expect(() => repeatStr(str, count)).toThrow("count must be non-negative");
5049
});

0 commit comments

Comments
 (0)