Skip to content

Commit 0347b66

Browse files
author
Ogbemi mene
committed
Merge branch 'Sprint-3/-Practice-TDD-Coursework-Exercises' of https://github.com/meneogbemi42-bit/Module-Structuring-and-Testing-Data into Sprint-3/-Practice-TDD-Coursework-Exercises
2 parents 8657ab8 + 4e621d4 commit 0347b66

2 files changed

Lines changed: 60 additions & 19 deletions

File tree

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,43 @@
1-
function getOrdinalNumber(num) {
2-
return "1st";
3-
}
1+
//function getOrdinalNumber(num) {
2+
//return "1st";
3+
//}
44

5-
module.exports = getOrdinalNumber;
5+
//module.exports = getOrdinalNumber;
66
// this is a code i came up with to get the ordinal number of a given number.
77
// I will now write tests to check if it works correctly.
88

9+
//function getOrdinalNumber(n) {
10+
//const pr = new Intl.PluralRules("en-US", { type: "ordinal" });
11+
//const suffixes = { one: "st", two: "nd", few: "rd", other: "th" };
12+
//const rule = pr.select(n);
13+
//return `${n}${suffixes[rule]}`;
14+
//}
15+
16+
// code without any API
17+
918
function getOrdinalNumber(n) {
10-
const pr = new Intl.PluralRules("en-US", { type: "ordinal" });
11-
const suffixes = { one: "st", two: "nd", few: "rd", other: "th" };
12-
const rule = pr.select(n);
13-
return `${n}${suffixes[rule]}`;
14-
}
19+
// Pure arithmetic to get absolute value without Math.abs()
20+
let positiveN;
21+
if (n < 0) {
22+
positiveN = -n;
23+
} else {
24+
positiveN = n;
25+
}
26+
27+
const lastTwoDigits = positiveN % 100;
28+
const lastDigit = positiveN % 10;
29+
30+
// Teens exception: 11th, 12th, 13th
31+
if (lastTwoDigits >= 11 && lastTwoDigits <= 13) {
32+
return `${n}th`;
33+
}
34+
35+
// Standard endings
36+
if (lastDigit === 1) return `${n}st`;
37+
if (lastDigit === 2) return `${n}nd`;
38+
if (lastDigit === 3) return `${n}rd`;
39+
40+
return `${n}th`;
41+
}
42+
43+
module.exports = getOrdinalNumber;

Sprint-3/2-practice-tdd/get-ordinal-number.test.js

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ test("should append 'st' for numbers ending with 1, except those ending with 11"
2121

2222
// here are more test cases for different scenarios
2323

24-
const getOrdinalNumber = require("./get-ordinal-number");
25-
2624
describe("getOrdinalNumber", () => {
2725
// Case 1: Already defined in your prompt
2826
test("should append 'st' for numbers ending with 1, except those ending with 11", () => {
@@ -55,12 +53,26 @@ describe("getOrdinalNumber", () => {
5553
expect(getOrdinalNumber(113)).toEqual("113th");
5654
});
5755

58-
// Case 5: All other numbers
59-
test("should append 'th' for all other numbers", () => {
60-
expect(getOrdinalNumber(4)).toEqual("4th");
61-
expect(getOrdinalNumber(10)).toEqual("10th");
62-
expect(getOrdinalNumber(20)).toEqual("20th");
63-
expect(getOrdinalNumber(34)).toEqual("34th");
64-
expect(getOrdinalNumber(100)).toEqual("100th");
56+
// Case 5: Numbers ending in 0, 4, 5, 6, 7, 8, or 9
57+
describe("numbers ending with 0, 4, 5, 6, 7, 8, or 9", () => {
58+
const defaultThCases = [
59+
{ endingDigit: 0, testValues: [0, 10, 20, 100, -10] },
60+
{ endingDigit: 4, testValues: [4, 24, 104, -4] },
61+
{ endingDigit: 5, testValues: [5, 25, 105, -5] },
62+
{ endingDigit: 6, testValues: [6, 26, 106, -6] },
63+
{ endingDigit: 7, testValues: [7, 27, 107, -7] },
64+
{ endingDigit: 8, testValues: [8, 28, 108, -8] },
65+
{ endingDigit: 9, testValues: [9, 29, 109, -9] },
66+
];
67+
68+
69+
defaultThCases.forEach(({ endingDigit, testValues }) => {
70+
test.each(testValues)(
71+
`appends "th" to %i (representative for digit ${endingDigit})`,
72+
(num) => {
73+
expect(getOrdinalNumber(num)).toBe(`${num}th`);
74+
}
75+
);
76+
});
6577
});
66-
});
78+
});

0 commit comments

Comments
 (0)