Skip to content

Commit ba0f73e

Browse files
committed
implement the the function getOrdinalNumber that works for any vali positive integer.
1 parent 047df5d commit ba0f73e

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

Sprint-3/3-mandatory-practice/implement/get-ordinal-number.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
1-
function getOrdinalNumber(num) {
2-
if (num === 1) {
3-
return "1st";
1+
function getOrdinalNumber(n) {
2+
let ord = "th";
3+
4+
if (n % 10 == 1 && n % 100 != 11) {
5+
ord = "st";
6+
} else if (n % 10 == 2 && n % 100 != 12) {
7+
ord = "nd";
8+
} else if (n % 10 == 3 && n % 100 != 13) {
9+
ord = "rd";
410
}
11+
12+
return ord;
513
}
614
console.log(getOrdinalNumber(1)); // The output should be "1st"
715

0 commit comments

Comments
 (0)