-
-
Notifications
You must be signed in to change notification settings - Fork 337
Expand file tree
/
Copy pathget-ordinal-number.js
More file actions
32 lines (28 loc) · 854 Bytes
/
get-ordinal-number.js
File metadata and controls
32 lines (28 loc) · 854 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/**
* getOrdinalNumber - Final Implementation
* --------------------------------------
* Converts a number to its English ordinal string (1st, 2nd, 3rd, etc.)
*/
function getOrdinalNumber(num) {
// 1. التعامل مع الاستثناءات (11, 12, 13)
// نستخدم % 100 للحصول على آخر رقمين
const lastTwoDigits = num % 100;
if (lastTwoDigits >= 11 && lastTwoDigits <= 13) {
return num + "th";
}
// 2. الحصول على آخر رقم في العدد
const lastDigit = num % 10;
// 3. تحديد النهاية بناءً على آخر رقم
switch (lastDigit) {
case 1:
return num + "st";
case 2:
return num + "nd";
case 3:
return num + "rd";
default:
return num + "th";
}
}
// تصدير الدالة للاختبار
module.exports = getOrdinalNumber;