Skip to content

Commit 833bffc

Browse files
committed
Made changes based on comments received.
1 parent f1bb8ed commit 833bffc

4 files changed

Lines changed: 24 additions & 15 deletions

File tree

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
function getOrdinalNumber(num) {
2-
let stringNum = num.toString();
3-
let lastDigit = stringNum[stringNum.length - 1];
4-
if (lastDigit === "1" && num % 100 !== 11) {
5-
return stringNum + "st";
6-
} else if (lastDigit === "2" && num % 100 !== 12) {
7-
return stringNum + "nd";
8-
} else if (lastDigit === "3" && num % 100 !== 13) {
9-
return stringNum + "rd";
2+
const lastDigit = num % 10;
3+
const lastTwoDigits = num % 100;
4+
5+
if (lastDigit === 1 && lastTwoDigits !== 11) {
6+
return num + "st";
7+
} else if (lastDigit === 2 && lastTwoDigits !== 12) {
8+
return num + "nd";
9+
} else if (lastDigit === 3 && lastTwoDigits !== 13) {
10+
return num + "rd";
1011
} else {
11-
return stringNum + "th";
12+
return num + "th";
1213
}
1314
}
1415

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,7 @@ test("should append 'rd' for numbers ending with 3, except those ending with 13"
4141
test("should append 'th' for all other numbers", () => {
4242
expect(getOrdinalNumber(4)).toEqual("4th");
4343
expect(getOrdinalNumber(11)).toEqual("11th");
44+
expect(getOrdinalNumber(12)).toEqual("12th");
45+
expect(getOrdinalNumber(35)).toEqual("35th");
46+
expect(getOrdinalNumber(100)).toEqual("100th");
4447
});
Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
function repeatStr(str, count) {
2-
if (count === 0) {
3-
return "";
4-
} else if (count > 0) {
5-
return str.repeat(count);
6-
} else {
2+
if (count < 0 || !Number.isInteger(count)) {
73
throw new Error("Count must be a non-negative integer");
84
}
9-
return "";
5+
6+
return count > 0 ? str.repeat(count) : "";
107
}
118

129
module.exports = repeatStr;

Sprint-3/2-practice-tdd/repeat-str.test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,11 @@ test("should throw an error when count is negative", () => {
4949
repeatStr(str, count);
5050
}).toThrow("Count must be a non-negative integer");
5151
});
52+
53+
test("should throw an error when count is not a number", () => {
54+
const str = "error";
55+
const count = "-2";
56+
expect(() => {
57+
repeatStr(str, count);
58+
}).toThrow("Count must be a non-negative integer");
59+
});

0 commit comments

Comments
 (0)