Skip to content

Commit 248d2a4

Browse files
committed
completed jest testing o and implemented the function as required
1 parent d85efd8 commit 248d2a4

2 files changed

Lines changed: 41 additions & 5 deletions

File tree

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,24 @@
1-
function repeatStr() {
2-
// 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).
3-
// The goal is to re-implement that function, not to use it.
4-
return "hellohellohello";
5-
}
1+
function repeatStr(word, wordCounter) {
2+
// 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).
3+
// The goal is to re-implement that function, not to use it.
4+
let count = 0;
5+
let repeatedString = []
66

7+
if (wordCounter < 0) { // rejects negative numbers
8+
throw new Error("Enter a positive number")
9+
}
10+
if(wordCounter === 0){
11+
return ""
12+
}
13+
if(wordCounter === 1){
14+
return word
15+
}
16+
if(wordCounter > 1){
17+
while(count < wordCounter){
18+
count++
19+
repeatedString.push(word)
20+
}
21+
}
22+
return repeatedString.join("")
23+
}
724
module.exports = repeatStr;

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,32 @@ test("should repeat the string count times", () => {
2020
// Given a target string `str` and a `count` equal to 1,
2121
// When the repeatStr function is called with these inputs,
2222
// Then it should return the original `str` without repetition.
23+
test("should repeat the string 1 time", () =>{
24+
const str = "name";
25+
const count = 1;
26+
const repeatedStr = repeatStr(str, count);
27+
expect(repeatedStr).toEqual("name")
28+
})
2329

2430
// Case: Handle count of 0:
2531
// Given a target string `str` and a `count` equal to 0,
2632
// When the repeatStr function is called with these inputs,
2733
// Then it should return an empty string.
34+
test("should return an empty string", () =>{
35+
const str = "name";
36+
const count = 0;
37+
const repeatedStr = repeatStr(str, count);
38+
expect(repeatedStr).toEqual("")
39+
})
2840

2941
// Case: Handle negative count:
3042
// Given a target string `str` and a negative integer `count`,
3143
// When the repeatStr function is called with these inputs,
3244
// Then it should throw an error, as negative counts are not valid.
45+
// Case: Handle negative count:
46+
// Given a target string `str` and a negative integer `count`,
47+
// When the repeatStr function is called with these inputs,
48+
// Then it should throw an error, as negative counts are not valid.
49+
test("throws on negative number", () => {
50+
expect(() => repeatStr("dami",-5)).toThrow("Enter a positive number");
51+
});

0 commit comments

Comments
 (0)