File tree Expand file tree Collapse file tree 1 file changed +39
-0
lines changed
Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ *
13function repeatStr() {
24 return "hellohellohello";
35}
46
57module.exports = repeatStr;
8+ *
9+ */
10+
11+ // repeat-str.js
12+
13+ function repeatStr ( str , count ) {
14+ // Convert count to a number
15+ const numCount = Number ( count ) ;
16+
17+ // Check if count is a valid number
18+ if ( isNaN ( numCount ) ) {
19+ throw new Error ( "Count must be a number" ) ;
20+ }
21+
22+ // Check for negative count
23+ if ( numCount < 0 ) {
24+ throw new Error ( "Count must be a positive integer" ) ;
25+ }
26+
27+ // Handle count of 0
28+ if ( numCount === 0 ) {
29+ return "" ;
30+ }
31+
32+ // Floor the count to handle decimal numbers
33+ const repeatTimes = Math . floor ( numCount ) ;
34+ let result = "" ;
35+
36+ // Build the repeated string
37+ for ( let i = 0 ; i < repeatTimes ; i ++ ) {
38+ result += str ;
39+ }
40+
41+ return result ;
42+ }
43+
44+ module . exports = repeatStr ;
You can’t perform that action at this time.
0 commit comments