Skip to content

Commit 1413394

Browse files
committed
refactor: throw error for negative count in repeat() to avoid ambiguous return values
1 parent 8d5db28 commit 1413394

File tree

3 files changed

+38
-40
lines changed

3 files changed

+38
-40
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
function getOrdinalNumber(num) {
2-
const twoLastDig = num % 100;
3-
if (twoLastDig === 11 || twoLastDig === 12 || twoLastDig === 13) {
2+
const itsTwoLastDigs = num % 100;
3+
if (itsTwoLastDigs === 11 || itsTwoLastDigs === 12 || itsTwoLastDigs === 13) {
44
return num + "th";
55
}
66

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

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8,36 +8,35 @@ 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");
13-
});
14-
test("should return '11th' for 11", () => {
15-
expect(getOrdinalNumber(11)).toEqual("11th");
16-
});
17-
18-
test("should return '2nd' for 2", () => {
19-
expect(getOrdinalNumber(2)).toEqual("2nd");
20-
});
21-
22-
23-
test("should return '23rd' for 23rd", () => {
24-
expect(getOrdinalNumber(23)).toEqual("23rd");
25-
});
26-
test("should return '3rd' for 3", () => {
27-
expect(getOrdinalNumber(3)).toEqual("3rd");
28-
});
29-
test("should return '4th' for 4", () => {
30-
expect(getOrdinalNumber(4)).toEqual("4th");
31-
});
32-
33-
test("should return '12th' for 12", () => {
34-
expect(getOrdinalNumber(12)).toEqual("12th");
35-
});
36-
test("should return '13th' for 13", () => {
37-
expect(getOrdinalNumber(13)).toEqual("13th");
38-
});
39-
40-
41-
test("should return '101st' for 101", () => {
42-
expect(getOrdinalNumber(101)).toEqual("101st");
11+
describe("getOrdinalNumber", () => {
12+
test("append 'st' to numbers ending in 1 expect 11", () => {
13+
expect(getOrdinalNumber(1)).toEqual("1st");
14+
expect(getOrdinalNumber(101)).toEqual("101st");
15+
});
16+
test("should return '11th' for 11", () => {
17+
expect(getOrdinalNumber(11)).toEqual("11th");
18+
});
19+
20+
test("append 'rd' to numbers ending in 3", () => {
21+
expect(getOrdinalNumber(3)).toEqual("3rd");
22+
expect(getOrdinalNumber(23)).toEqual("23rd");
23+
expect(getOrdinalNumber(33)).toEqual("33rd");
24+
expect(getOrdinalNumber(103)).toEqual("103rd");
25+
});
26+
test("should return '4th' for 4", () => {
27+
expect(getOrdinalNumber(4)).toEqual("4th");
28+
});
29+
30+
test("should return '12th' for 12", () => {
31+
expect(getOrdinalNumber(12)).toEqual("12th");
32+
});
33+
test("should return '13th' for 13", () => {
34+
expect(getOrdinalNumber(13)).toEqual("13th");
35+
});
36+
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+
});
4342
});
Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
function repeat(str,count) {
2-
if (count=== 0){
3-
return ""
4-
}
5-
if(count<0){
6-
return "invalid"
1+
function repeat(str, count) {
2+
if (count < 0) {
3+
throw new Error("count must be non-negative");
74
}
85
return str.repeat(count);
96
}
107

118
module.exports = repeat;
9+
10+

0 commit comments

Comments
 (0)