Skip to content

Commit 3e9b02e

Browse files
committed
added repeat fuction and comments of how code is working
1 parent ce6c536 commit 3e9b02e

File tree

2 files changed

+51
-41
lines changed

2 files changed

+51
-41
lines changed

Sprint-3/revise/implement/repeat.js

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,27 +24,30 @@
2424
// When the repeat function is called with these inputs,
2525
// Then it should throw an error or return an appropriate error message, as negative counts are not valid.
2626

27-
function repeatString(str, count){
28-
let newWords = "";
29-
//if count is greater than 1 if so repeat string count times
30-
//else if count is 1 return the string
31-
//else if count is 0 return empty string
32-
//else if count is negative return error
33-
if(count > 1){
34-
for(let i = 0; i < count; i++){
35-
newWords = newWords + str + " "
36-
37-
}
38-
return newWords.trimEnd();
27+
function repeatString(str, count) {
28+
let newWords = "";
29+
//if count is greater than 1 if so repeat string count times
30+
//else if count is 1 return the string
31+
//else if count is 0 return empty string
32+
//else if count is negative return error
33+
if (count > 1) {
34+
for (let i = 0; i < count; i++) {
35+
newWords += str + " ";
3936
}
40-
else if(count === 0){
41-
return "";
42-
}
43-
else if(count < 0 ){
44-
console.error("error your number is negative");
45-
46-
}
47-
37+
return newWords.trimEnd();
38+
}
39+
// Si count === 1, retorna el string original
40+
else if (count === 1) {
41+
return str;
42+
}
43+
// Si count === 0, retorna una cadena vacía
44+
else if (count === 0) {
45+
return "";
46+
}
47+
// Si count < 0, retorna un mensaje de error
48+
else if (count < 0) {
49+
return "error your number is negative";
50+
}
4851
}
4952

50-
console.log(repeatString("karla", -1))
53+
module.exports = repeatString;
Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,32 @@
1-
// Implement a function repeat
1+
const repeatString = require("./repeat");
22

3-
// Given a target string str and a positive integer count,
4-
// When the repeat function is called with these inputs,
5-
// Then it should:
3+
function assertEquals(actualOutput, targetOutput) {
4+
console.assert(
5+
actualOutput === targetOutput,
6+
`Expected "${actualOutput}" to equal "${targetOutput}"`
7+
);
8+
}
69

7-
// case: repeat String:
8-
// Given a target string str and a positive integer count,
9-
// When the repeat function is called with these inputs,
10-
// Then it should repeat the str count times and return a new string containing the repeated str values.
10+
describe("repeatString Function Tests", () => {
11+
test("Repeats string 3 times", () => {
12+
const result = repeatString("hello", 3);
13+
assertEquals(result, "hello hello hello");
14+
});
1115

12-
// case: handle Count of 1:
13-
// Given a target string str and a count equal to 1,
14-
// When the repeat function is called with these inputs,
15-
// Then it should return the original str without repetition, ensuring that a count of 1 results in no repetition.
16+
test("Repeats string 1 time", () => {
17+
const result = repeatString("world", 1);
18+
assertEquals(result, "world");
19+
});
1620

17-
// case: Handle Count of 0:
18-
// Given a target string str and a count equal to 0,
19-
// When the repeat function is called with these inputs,
20-
// Then it should return an empty string, ensuring that a count of 0 results in an empty output.
21+
test("Repeats string 0 times", () => {
22+
const result = repeatString("test", 0);
23+
assertEquals(result, "");
24+
});
2125

22-
// case: Negative Count:
23-
// Given a target string str and a negative integer count,
24-
// When the repeat function is called with these inputs,
25-
// Then it should throw an error or return an appropriate error message, as negative counts are not valid.
26+
test("Handles negative count", () => {
27+
const result = repeatString("errorTest", -2);
28+
assertEquals(result, "error your number is negative");
29+
});
30+
});
31+
32+
console.log("All test cases executed!");

0 commit comments

Comments
 (0)