Skip to content

Commit 0463407

Browse files
the new code without API( application programm interface).
1 parent 0e7fe70 commit 0463407

1 file changed

Lines changed: 38 additions & 9 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;

0 commit comments

Comments
 (0)