Skip to content

Commit 4b70646

Browse files
committed
implement the function getOrdinalNumber
1 parent eb8edda commit 4b70646

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed
Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
function getOrdinalNumber(num) {
2-
return "1st";
2+
if (num % 10 === 1 && num % 100 !== 11) {
3+
// we also can use if (num[-1]===1 && num.slice(-2)!==11)
4+
return num + "st";
5+
} else if (num % 10 === 2 && num % 100 !== 12) {
6+
// we also can use if (num[-1]===2 && num.slice(-2)!==12)
7+
return num + "nd";
8+
} else if (num % 10 === 3 && num % 100 !== 13) {
9+
// we also can use if (num[-1]===3 && num.slice(-2)!==13)
10+
return num + "rd";
11+
} else {
12+
return num + "th";
13+
}
314
}
415

516
module.exports = getOrdinalNumber;

0 commit comments

Comments
 (0)