Skip to content

Commit 5202e76

Browse files
sprint 3 task 2 exercise 2 completed
1 parent 96408b3 commit 5202e76

2 files changed

Lines changed: 35 additions & 2 deletions

File tree

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,19 @@
1-
function repeatStr() {
1+
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-
return "hellohellohello";
4+
if (count < 0) {
5+
return "Invalid count";
6+
}
7+
8+
let new_String = "";
9+
10+
for (let counter = 0; counter < count; counter++) {
11+
new_String = str + new_String;
12+
}
13+
14+
return new_String;
515
}
616

17+
console.log(repeatStr("hell", -1));
18+
719
module.exports = repeatStr;

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,33 @@ test("should repeat the string count times", () => {
2121
// When the repeatStr function is called with these inputs,
2222
// Then it should return the original `str` without repetition.
2323

24+
test("should return the original string without repetition", () => {
25+
const str = "hello";
26+
const count = 1;
27+
const repeatedStr = repeatStr(str, count);
28+
expect(repeatedStr).toEqual("hello");
29+
});
30+
2431
// Case: Handle count of 0:
2532
// Given a target string `str` and a `count` equal to 0,
2633
// When the repeatStr function is called with these inputs,
2734
// Then it should return an empty string.
2835

36+
test("should return an empty string", () => {
37+
const str = "hello";
38+
const count = 0;
39+
const repeatedStr = repeatStr(str, count);
40+
expect(repeatedStr).toEqual("");
41+
});
42+
2943
// Case: Handle negative count:
3044
// Given a target string `str` and a negative integer `count`,
3145
// When the repeatStr function is called with these inputs,
3246
// Then it should throw an error, as negative counts are not valid.
47+
48+
test('When count negative should return "Invalid count"', () => {
49+
const str = "hello";
50+
const count = -1;
51+
const repeatedStr = repeatStr(str, count);
52+
expect(repeatedStr).toEqual("Invalid count");
53+
});

0 commit comments

Comments
 (0)