Skip to content

Commit 2ff02a6

Browse files
committed
implement recommendation from PR
1 parent e3675ae commit 2ff02a6

File tree

3 files changed

+43
-59
lines changed

3 files changed

+43
-59
lines changed

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

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,34 +8,31 @@ 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", () => {
11+
test("append 'st' to numbers ending in 1, except those ending in 11", () => {
1212
expect(getOrdinalNumber(1)).toEqual("1st");
13+
expect(getOrdinalNumber(101)).toEqual("101st");
1314
});
1415

15-
//identify ordinal for 2
16-
test("should return '2nd' for 2", () => {
16+
//identify ordinal for nd
17+
test("append 'nd' to numbers ending in 2, except those ending in 12", () => {
1718
expect(getOrdinalNumber(2)).toEqual("2nd");
19+
expect(getOrdinalNumber(132)).toEqual("132nd");
1820
});
1921

20-
//identify ordinal for 3
21-
test("should return '3rd' for 3", () => {
22+
//identify ordinal for rd
23+
test("append 'rd' to numbers ending in 3, except those ending in 13", () => {
2224
expect(getOrdinalNumber(3)).toEqual("3rd");
23-
});
24-
25-
//identify ordinal for 4
26-
test("should return '4th' for 4", () => {
27-
expect(getOrdinalNumber(4)).toEqual("4th");
25+
expect(getOrdinalNumber(23)).toEqual("23rd");
2826
});
2927

3028
//Special English Rule... 12, 13, 14 ending in "th"
31-
test("should return '11th' for 11", () => {
29+
test("append 'th' to numbers that end in 11, 12, or 13", () => {
3230
expect(getOrdinalNumber(11)).toEqual("11th");
31+
expect(getOrdinalNumber(113)).toEqual("113th");
3332
});
3433

35-
test("should return '12th' for 12", () => {
36-
expect(getOrdinalNumber(12)).toEqual("12th");
37-
});
38-
39-
test("should return '13th' for 13", () => {
40-
expect(getOrdinalNumber(13)).toEqual("13th");
34+
//All other numbers => th
35+
test("append 'th' to all other numbers", () => {
36+
expect(getOrdinalNumber(4)).toEqual("4th");
37+
expect(getOrdinalNumber(100)).toEqual("100th");
4138
});

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

Lines changed: 11 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,14 @@
1-
function repeat() {
2-
if (arguments[1] < 0) throw new Error("Count must be non-negative");
3-
return arguments[0].repeat(arguments[1]);
1+
function repeat(str, count) {
2+
if (count < 0) {
3+
throw new Error("Count must be non-negative");
4+
}
5+
6+
let result = "";
7+
for (let i = 0; i < count; i++) {
8+
result += str;
9+
}
10+
return result;
411
}
5-
//other implementation
6-
// function repeat(str, count) {
7-
// if (count < 0) {
8-
// throw new Error("Count must be non-negative");
9-
// }
10-
11-
// let result = "";
12-
// for (let i = 0; i < count; i++) {
13-
// result += str;
14-
// }
15-
// return result;
16-
// }
17-
18-
// module.exports = repeat;
19-
20-
// function repeat() {
21-
// const str = arguments[0];
22-
// const count = arguments[1];
23-
24-
// if (count < 0) {
25-
// throw new Error("Count must be non-negative");
26-
// }
27-
28-
// let output = "";
29-
// for (let i = 0; i < count; i++) {
30-
// output += str;
31-
// }
32-
33-
// return output;
34-
// }
3512

3613
module.exports = repeat;
14+

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

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,28 @@
22

33
const repeat = require("./repeat");
44

5-
test("should repeat the string count times", () => {
6-
expect(repeat("hello", 3)).toEqual("hellohellohello");
7-
});
8-
9-
test("should return the original string when count is 1", () => {
10-
expect(repeat("hello", 1)).toEqual("hello");
5+
// case: handle Count of 1:
6+
// When count = 1, return the original string unchanged
7+
test("should return original string when count is 1", () => {
8+
const str = "hello";
9+
const count = 1;
10+
const repeatedStr = repeat(str, count);
11+
expect(repeatedStr).toEqual("hello");
1112
});
1213

14+
// case: Handle Count of 0:
15+
// When count = 0, return empty string
1316
test("should return an empty string when count is 0", () => {
14-
expect(repeat("hello", 0)).toEqual("");
17+
const str = "world";
18+
const count = 0;
19+
const repeatedStr = repeat(str, count);
20+
expect(repeatedStr).toEqual("");
1521
});
1622

23+
// case: Negative Count:
24+
// When count < 0, throw an error
1725
test("should throw an error when count is negative", () => {
18-
expect(() => repeat("hello", -2)).toThrow("Count must be non-negative");
26+
const str = "test";
27+
const count = -3;
28+
expect(() => repeat(str, count)).toThrow("Count must be non-negative");
1929
});
20-

0 commit comments

Comments
 (0)