File tree Expand file tree Collapse file tree 1 file changed +23
-10
lines changed
Expand file tree Collapse file tree 1 file changed +23
-10
lines changed Original file line number Diff line number Diff line change 11function repeat ( valueToRepeat , numOfTimes ) {
2+ // Validate count
3+ if ( ! Number . isInteger ( numOfTimes ) ) {
4+ return "Invalid count: count should be an integer" ;
5+ }
6+ if ( numOfTimes < 0 ) {
7+ return "Negative number invalid" ;
8+ }
9+ if ( numOfTimes === 0 ) {
10+ return "" ;
11+ }
12+
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 ) ;
20+
21+ // Repeat the string
222 let repeatedValue = "" ;
3- if ( numOfTimes > 0 && Number . isInteger ( numOfTimes ) ) {
4- for ( let i = 0 ; i < numOfTimes ; i ++ ) {
5- repeatedValue += valueToRepeat ;
6- }
7- } else if ( numOfTimes === 0 ) {
8- repeatedValue = "" ;
9- } else if ( numOfTimes < 0 ) {
10- repeatedValue = "Negative number invalid" ;
11- } else if ( ! Number . isInteger ( numOfTimes ) ) {
12- repeatedValue = "Invalid count: count should be an integer"
23+ for ( let i = 0 ; i < numOfTimes ; i ++ ) {
24+ repeatedValue += strValue ;
1325 }
26+
1427 return repeatedValue ;
1528}
1629
You can’t perform that action at this time.
0 commit comments