Skip to content

Commit adb9421

Browse files
committed
Fix function to allow only string for valueToRepeat and positive integer for numOfTimes; all other inputs return an error message
1 parent 3243748 commit adb9421

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed

Sprint-3/2-practice-tdd/repeat.js

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,25 @@
11
function repeat(valueToRepeat, numOfTimes) {
2+
// Validate valueToRepeat
3+
if (typeof valueToRepeat !== "string") {
4+
return "Invalid input: valueToRepeat should be a string";
5+
}
26
// Validate numOfTimes
37
if (!Number.isInteger(numOfTimes)) {
4-
return "Invalid numOfTimes: numOfTimes should be an integer";
8+
return "Invalid input: numOfTimes should be an integer";
59
}
10+
611
if (numOfTimes < 0) {
712
return "Negative number invalid";
813
}
914
if (numOfTimes === 0) {
1015
return "";
1116
}
1217

13-
// Convert arrays to empty string
14-
if (Array.isArray(valueToRepeat)) {
15-
valueToRepeat = "";
16-
}
17-
18-
// Convert other types to string
19-
const strValue = String(valueToRepeat);
2018

2119
// Repeat the string
2220
let repeatedValue = "";
2321
for (let i = 0; i < numOfTimes; i++) {
24-
repeatedValue += strValue;
22+
repeatedValue += valueToRepeat;
2523
}
2624

2725
return repeatedValue;

0 commit comments

Comments
 (0)