Skip to content

Commit c17eaa5

Browse files
committed
Some refactoring
1 parent d9fa7a9 commit c17eaa5

File tree

2 files changed

+25
-20
lines changed

2 files changed

+25
-20
lines changed
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
function getOrdinalNumber(num) {
22
let result;
3-
let rem_100 = num % 100;
4-
let rem_10 = num % 10;
5-
if (rem_100 == 11 || rem_100 == 12 || rem_100 == 13){
3+
let lastTwoDigits = num % 100;
4+
let lastDigit = num % 10;
5+
if (lastTwoDigits == 11 || lastTwoDigits == 12 || lastTwoDigits == 13){
66
result = num.toString() + "th";
77
}
8-
else if (rem_10 == 1){
8+
else if (lastDigit == 1){
99
result = num.toString() +"st";
1010
}
11-
else if (rem_10 == 2){
11+
else if (lastDigit == 2){
1212
result = num.toString() + "nd";
1313
}
14-
else if (rem_10 == 3){
14+
else if (lastDigit == 3){
1515
result = num.toString() + "rd";
1616
}
1717
else {
1818
result = num.toString() + "th";
1919
}
2020

21-
return result
21+
return result;
2222
}
2323
module.exports = getOrdinalNumber;

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

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,30 @@ const getOrdinalNumber = require("./get-ordinal-number");
88
// When the number is 1,
99
// Then the function should return "1st"
1010

11-
test("should return '1st' for 1", () => {
12-
expect(getOrdinalNumber(1)).toEqual("1st");
11+
test("append 'st' to numbers ending in 1, except those ending in 11", () => {
12+
expect( getOrdinalNumber(1) ).toEqual("1st");
13+
expect( getOrdinalNumber(21) ).toEqual("21st");
14+
expect( getOrdinalNumber(131) ).toEqual("131st");
1315
});
1416

15-
test("should return '2nd' for 2", () => {
16-
expect(getOrdinalNumber(2)).toEqual("2nd");
17+
test("append 'nd' to numbers ending in 2, except those ending in 12", () => {
18+
expect( getOrdinalNumber(2) ).toEqual("2nd");
19+
expect( getOrdinalNumber(22) ).toEqual("22nd");
20+
expect( getOrdinalNumber(132) ).toEqual("132nd");
1721
});
1822

19-
test("should return '3rd' for 3", () => {
20-
expect(getOrdinalNumber(3)).toEqual("3rd");
23+
test("append 'rd' to numbers ending in 3, except those ending in 13", () => {
24+
expect( getOrdinalNumber(3) ).toEqual("3rd");
25+
expect( getOrdinalNumber(23) ).toEqual("23rd");
26+
expect( getOrdinalNumber(133) ).toEqual("133rd");
2127
});
2228

23-
test("should return '4th' for 4", () => {
24-
expect(getOrdinalNumber(4)).toEqual("4th");
29+
test("append 'th' to numbers ending in 4 and more", () => {
30+
expect( getOrdinalNumber(4) ).toEqual("4th");
31+
expect( getOrdinalNumber(25) ).toEqual("25th");
32+
expect( getOrdinalNumber(139) ).toEqual("139th");
2533
});
34+
2635
test("should return '11th' for 11", () => {
2736
expect(getOrdinalNumber(11)).toEqual("11th");
2837
});
@@ -34,8 +43,4 @@ test("should return '12th' for 12", () => {
3443
test("should return '13th' for 13", () => {
3544
expect(getOrdinalNumber(13)).toEqual("13th");
3645
});
37-
test("append 'nd' to numbers ending in 2, except those ending in 12", () => {
38-
expect( getOrdinalNumber(2) ).toEqual("2nd");
39-
expect( getOrdinalNumber(22) ).toEqual("22nd");
40-
expect( getOrdinalNumber(132) ).toEqual("132nd");
41-
});
46+

0 commit comments

Comments
 (0)