Skip to content

Commit f3d3d7c

Browse files
committed
Added code and test to throw an error.. it is at this time I was reading the task again and see I must not use .repeat.... here we go again then ^_^
1 parent 7ad95cf commit f3d3d7c

2 files changed

Lines changed: 12 additions & 4 deletions

File tree

‎practice-tdd/repeat-str.js‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
export 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+
if (count < 0) {
5+
throw new Error("negative counts are not valid");
6+
}
47
return str.repeat(count);
58
}

‎practice-tdd/repeat-str.test.js‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ import { repeatStr } from "./repeat-str.js";
88
// Given a target string `str` and a positive integer `count` greater than 1,
99
// When the repeatStr function is called with these inputs,
1010
// Then it should return a string that contains the original `str` repeated `count` times.
11-
12-
test("should repeat the string count times", () => {
11+
test("should repeat the string count 3 times", () => {
1312
const str = "hello";
1413
const count = 3;
1514
const repeatedStr = repeatStr(str, count);
@@ -20,7 +19,7 @@ test("should repeat the string count times", () => {
2019
// Given a target string `str` and a `count` equal to 1,
2120
// When the repeatStr function is called with these inputs,
2221
// Then it should return the original `str` without repetition.
23-
test("should repeat the string count times", () => {
22+
test("should repeat the string count 1 times", () => {
2423
const str = "hello";
2524
const count = 1;
2625
const repeatedStr = repeatStr(str, count);
@@ -31,7 +30,7 @@ test("should repeat the string count times", () => {
3130
// Given a target string `str` and a `count` equal to 0,
3231
// When the repeatStr function is called with these inputs,
3332
// Then it should return an empty string.
34-
test("should repeat the string count times", () => {
33+
test("should repeat the string count 0 times", () => {
3534
const str = "hello";
3635
const count = 0;
3736
const repeatedStr = repeatStr(str, count);
@@ -42,3 +41,9 @@ test("should repeat the string count times", () => {
4241
// Given a target string `str` and a negative integer `count`,
4342
// When the repeatStr function is called with these inputs,
4443
// Then it should throw an error, as negative counts are not valid.
44+
test("should throw an error 'negative counts are not valid'", () => {
45+
const str = "hello";
46+
const count = -3;
47+
expect(() => repeatedStr(str, count)).toThrow;
48+
new Error("negative counts are not valid");
49+
});

0 commit comments

Comments
 (0)