Skip to content

Commit

Permalink
JS1-week4 | implement folder: repeat.test.js
Browse files Browse the repository at this point in the history
  • Loading branch information
pedram-am committed Dec 18, 2023
1 parent 2f5b936 commit 382453e
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions week-4/implement/repeat.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,44 @@
// 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.

function repeat(str, count) {
if (typeof str !== "string") {
throw new Error("The first parameter must be a string!");
}

if (typeof count !== "number") {
throw new Error("The second parameter must be a number!");
}

if (Number.isInteger(count) !== true || count < 0) {
throw new Error("Count must be a positive integer!");
}

let result = "";
for (let i = 0; i < count; i++) {
result += str;
}

return result;
}

test("repeats given string count times", function () {
expect(repeat("Hi", 3)).toBe("HiHiHi");
expect(repeat("Hi", 1)).toBe("Hi");
expect(repeat("Hi", 0)).toBe("");
expect(() => repeat(3, 3)).toThrow("The first parameter must be a string!");
expect(() => repeat("Hi", true)).toThrow(
"The second parameter must be a number!"
);
expect(() => repeat("Hi", -3)).toThrow("Count must be a positive integer!");
expect(() => repeat("Hi", 1.5)).toThrow("Count must be a positive integer!");
});

// console.log(repeat("Hi", 3));
// console.log(repeat("Hi", 1));
// console.log(repeat("Hi", 0));
// console.log(repeat("Hi", -3));
// console.log(repeat("Hi", 1.5));
// console.log(repeat(3, 3));
// console.log(repeat("Hi", true));

0 comments on commit 382453e

Please sign in to comment.