Skip to content

Commit 7fca66b

Browse files
author
Arthur
committed
test added
1 parent 2141dfb commit 7fca66b

2 files changed

Lines changed: 40 additions & 14 deletions

File tree

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
11
function repeatStr(str, count) {
22
// Your implementation of this function must *not* call String.prototype.repeat (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/repeat).
33
// The goal is to re-implement that function, not to use it.
4-
let repeatedStr= "";
5-
6-
for(i=0; i<count; i++){
7-
repeatedStr= repeatedStr + str;
8-
};
9-
return repeatedStr;}
104

5+
if (count < 0) {
6+
throw new Error("Oh no! Negative numbers are not allowed!");
7+
8+
}
119

10+
let repeatedStr = "";
1211

12+
for (i = 0; i < count; i++) {
13+
repeatedStr = repeatedStr + str;
14+
}
15+
return repeatedStr;
16+
}
1317

1418
module.exports = repeatStr;
1519

16-
17-
18-
19-
20-
21-
22-
//for committing//
20+
//for committing//

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

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ const repeatStr = require("./repeat-str");
44
// When the repeatStr function is called with these inputs,
55
// Then it should:
66

7+
test("Given a target string 'str' and a positive integer 'count'", () => {
8+
const str = "hello";
9+
const count = 3;
10+
const repeatedStr = repeatStr(str, count);
11+
expect(repeatedStr).toEqual("hellohellohello");
12+
});
13+
714
// Case: handle multiple repetitions:
815
// Given a target string `str` and a positive integer `count` greater than 1,
916
// When the repeatStr function is called with these inputs,
@@ -21,14 +28,35 @@ test("should repeat the string count times", () => {
2128
// When the repeatStr function is called with these inputs,
2229
// Then it should return the original `str` without repetition.
2330

31+
test("Given a target string `str` and a `count` equal to 1", () => {
32+
const str = "hello";
33+
const count = 1;
34+
const repeatedStr = repeatStr(str, count);
35+
expect(repeatedStr).toEqual("hello");
36+
});
37+
2438
// Case: Handle count of 0:
2539
// Given a target string `str` and a `count` equal to 0,
2640
// When the repeatStr function is called with these inputs,
2741
// Then it should return an empty string.
2842

43+
test("Given a target string `str` and a `count` equal to 1", () => {
44+
const str = "";
45+
const count = 0;
46+
const repeatedStr = repeatStr(str, count);
47+
expect(repeatedStr).toEqual("");
48+
});
49+
2950
// Case: Handle negative count:
3051
// Given a target string `str` and a negative integer `count`,
3152
// When the repeatStr function is called with these inputs,
3253
// Then it should throw an error, as negative counts are not valid.
3354

34-
//for committing//
55+
test(" Given a target string `str` and a negative integer `count` ", () => {
56+
const str = "idea";
57+
const count = -3;
58+
59+
expect(() => repeatStr("idea", -3)).toThrow(
60+
"Oh no! Negative numbers are not allowed!"
61+
);
62+
});

0 commit comments

Comments
 (0)