-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7728a16
commit 3933038
Showing
3 changed files
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/** | ||
@name repeat | ||
@description repeats a string by defined times. | ||
@param {item} item - _none_ - The input string/number to be repeated. | ||
@param {number} times - 1 - The number of times to repeat the string. | ||
@param {string} delimiter - "" - The delimiter to be used between the repeated strings. | ||
@returns {string} - The repeated string. | ||
@throws {Error} - Throws an error if the input is not a string. | ||
@example repeat("san"); // Returns "san" | ||
@example repeat("san", 4); // Returns "sansansansan" | ||
@example repeat("test ", 2); // Returns "test test" | ||
*/ | ||
|
||
function repeat(item, times = 1, delimiter = "") { | ||
if (!item) { | ||
throw new Error("Invalid string served"); | ||
} | ||
|
||
return new Array(times).fill(item).join(delimiter); | ||
} | ||
|
||
module.exports = repeat; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
const repeat = require("./index"); | ||
|
||
describe("repeat()", () => { | ||
test("Test that the function throws an error if an invalid input is provided", () => { | ||
expect(() => repeat(null)).toThrow(Error); | ||
expect(() => repeat(undefined)).toThrow(Error); | ||
}); | ||
|
||
test("Test that the function correctly repeats a single word", () => { | ||
expect(repeat("test")).toBe("test"); | ||
expect(repeat("testing", 2)).toBe("testingtesting"); | ||
expect(repeat("testing", 2, " ")).toBe("testing testing"); | ||
}); | ||
}); |