Skip to content

ZA | MAY-2025 | Shannon Davids | Sprint-3 | Module-Structuring-and-Testing-Data #616

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
repeat test
  • Loading branch information
Shannon-Davids committed Jun 26, 2025
commit 8be889d5dd01c61cb5e5c848443a4654afdb8bdd
11 changes: 8 additions & 3 deletions Sprint-3/3-mandatory-practice/implement/repeat.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
function repeat() {
return "hellohellohello";
function repeat(str, count) {
if (count === 0) return "";
if (count < 0) {
throw new Error("Not Allowed");
} else {
return str.repeat(count);
}
}

module.exports = repeat;
module.exports = repeat;
27 changes: 20 additions & 7 deletions Sprint-3/3-mandatory-practice/implement/repeat.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,36 @@ const repeat = require("./repeat");
// Then it should repeat the str count times and return a new string containing the repeated str values.

test("should repeat the string count times", () => {
const str = "hello";
const count = 3;
const repeatedStr = repeat(str, count);
expect(repeatedStr).toEqual("hellohellohello");
});
const str = "hello";
const count = 3;
const repeatedStr = repeat(str, count);
expect(repeatedStr).toEqual("hellohellohello");
});

// case: handle Count of 1:
// Given a target string str and a count equal to 1,
// When the repeat function is called with these inputs,
// Then it should return the original str without repetition, ensuring that a count of 1 results in no repetition.

test("should return original string", () => {
const str = "word";
const count = 1;
const repeatedStr = repeat(str, count);
expect(repeatedStr).toEqual("word");
});
// case: Handle Count of 0:
// Given a target string str and a count equal to 0,
// When the repeat function is called with these inputs,
// Then it should return an empty string, ensuring that a count of 0 results in an empty output.

test("should return empty string ", () => {
const str = "shannon";
const count = 0;
const repeatedStr = repeat(str, count);
expect(repeatedStr).toEqual("");
});
// case: Negative Count:
// Given a target string str and a negative integer count,
// When the repeat function is called with these inputs,
// Then it should throw an error or return an appropriate error message, as negative counts are not valid.
test("should return error", () => {
expect(() => repeat("hello", -4)).toThrow("Not Allowed");
});