Skip to content

Commit 72f52b6

Browse files
refactor: replace repeated expressions with descriptive variables in getOrdinalNumber
1 parent f9d0034 commit 72f52b6

File tree

1 file changed

+6
-10
lines changed

1 file changed

+6
-10
lines changed

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

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
function getOrdinalNumber(num) {
2-
if( num%10 === 1 && num%100 !== 11)
3-
{
2+
const lastDigit = num % 10;
3+
const lastTwoDigits = num % 100;
4+
if (lastDigit === 1 && lastTwoDigits !== 11) {
45
return `${num}st`;
5-
}
6-
else if (num%10 === 2 && num%100 !== 12)
7-
{
6+
} else if (lastDigit === 2 && lastTwoDigits !== 12) {
87
return `${num}nd`;
9-
}
10-
else if(num%10===3 && num%100 !==13)
11-
{
8+
} else if (lastDigit === 3 && lastTwoDigits !== 13) {
129
return `${num}rd`;
13-
}
14-
else{
10+
} else {
1511
return `${num}th`;
1612
}
1713
}

0 commit comments

Comments
 (0)