Repeat a string repeat a string [update]

Note 7.10.16: Just a little while ago, I placed the code of the solution into Code Beautify, a JS validator, instead of JS Beautify, a JS formatter. I noticed that I had made an error similar to another one in the solution to the Smallest Common Multiple algorithm. Code Beautify told me to move declaration of the variable i and newStr to the top of the function repeatStringNumTimes(str, num). I usually end up declaring the counter i within the for loop, because it visually makes sense for me, but I do see the point of moving it to the top of the function. The same reasoning went into declaration of newStr. Again, however, I do see the logic and reasoning as to why it should also be moved to the top! So that they both would be available to the whole body of the function. Please see new changes at the bottom of this post.

There are a couple of other issues that came up in the Code Beautify JS validator that conflicted with the formatting by JS Beautify. I will be looking into them later today, and will update here when I find the answers!

The solution to this algorithm is fairly straightforward. For those of you following my blog, parts of it will look very familiar. Note: when you start doing a lot of algorithms, and building a lot of things, you will recognize patterns/repetitions in your code. This will mean you are ready for the next level: modularization of your code. An integral part of code efficiency, and the ability to extract pieces and replace them with others. This is especially useful when you start applying frameworks and modules to your projects.

Getting back to Repeat a string repeat a string…

First, I knew I should take into account all possibilities of num. That num could be greater than 0, 0, or less than 0.

Next, I knew I had to use a conditional statement to solve the algorithm. Since it was an if else situation, I went with the if else statement.

Third, as with all if else statements, I always start with the most specific condition down to the most general. I started the solution with:

JavaScript

1

2

3

4

5

6

functionrepeatStringNumTimes(str,num){

if(num===0){

returnstr;

}

}

So if num === 0, return 1 instance of string. You can’t get more specific than that.

Next, I had to take into account num > 0. Since this meant repeating a string, to me it meant use of a for loop and then adding repetitions of str to a newStr:

JavaScript

1

2

3

4

5

6

7

8

9

10

11

functionrepeatStringNumTimes(str,num){

if(num===0){

returnstr;

}elseif(num>0){

for(vari=0;i<str.length;i++){

varnewStr="";

newStr+=str;

}

returnnewStr;

}

}

So if num > 0, the for loop iterates over the length of str, and with each iteration of the for loop, str is added to the variable newStr. When i is equal to the length of str, then the loop stops, and newStr is returned containing the repeated str num times.

Read my post in which I articulate my code for Reverse A String for an almost identical code pattern.

Last, I had to create the condition for when num is a negative number:

JavaScript

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

functionrepeatStringNumTimes(str,num){

"use strict";

// repeat after me

if(num===0){

returnstr;

}elseif(num>0){

varnewStr="";

for(vari=0;i<num;i++){

newStr+=str;

}

returnnewStr;

}else{

return"";

}

}

repeatStringNumTimes("abc",3);

Being the last condition to check for, and therefore the else statement at the end of the if else statements, it is the most general. Since I have already taken care of if (num === 0), most specific, and else if (num > 0), a bit less specific, the only thing left was else. else can only refer to num < 0. So in the case that num < 0, an empty string is returned.

JSHint Metrics:

JSHint Metrics for Repeat a string repeat a string

JavaScript

1

2

3

4

There isonly one functioninthisfile.

It takes2arguments.

Thisfunctioncontains7statements.

Cyclomatic complexity number forthisfunctionis4.

Original Solution:

Repeat a string repeat a string solution

JavaScript

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

functionrepeatStringNumTimes(str,num){

"use strict";

// repeat after me

if(num===0){

returnstr;

}elseif(num>0){

varnewStr="";

for(vari=0;i<num;i++){

newStr+=str;

}

returnnewStr;

}else{

return"";

}

}

repeatStringNumTimes("abc",3);

Original Solution in ES6:

Repeat a string repeat a string in ES6

JavaScript

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

functionrepeatStringNumTimes(str,num){

// repeat after me

if(num===0){

returnstr;

}elseif(num>0){

let newStr="";

for(leti=0;i<num;i++){

newStr+=str;

}

returnnewStr;

}else{

return"";

}

}

repeatStringNumTimes("abc",3);

Tweaked Solution:

JavaScript

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

functionrepeatStringNumTimes(str,num){

"use strict";

varnewStr="",

i;

// repeat after me

if(num===0){

returnstr;

}elseif(num>0){

for(i=0;i<num;i++){

newStr+=str;

}

returnnewStr;

}else{

return"";

}

}

repeatStringNumTimes("abc",3);

Tweaked Solution in ES6:

Tweaked Solution for Repeat a string repeat a string in ES6

JavaScript

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

functionrepeatStringNumTimes(str,num){

let newStr="",

i;

// repeat after me

if(num===0){

returnstr;

}elseif(num>0){

for(i=0;i<num;i++){

newStr+=str;

}

returnnewStr;

}else{

return"";

}

}

repeatStringNumTimes("abc",3);

Share this:

Like this:

Related

Maria Campbell

Founder, Inter-Global Media Network, Inc., where I focus on Front End Development. as well as a digital photographer, videographer, blogger, broadcaster, and graphic designer. Genesis Framework user. I got my start 8+ years ago teaching myself HTML/CSS and Wordpress Development because I couldn't find anyone who could create what I wanted. I have been working with open source software ever since. I am an open source software evangelist, and a "Women in Technology" evangelist. A creative professional myself, I find Web Development to be both practical and creative. I love to cook, travel, read mysteries, love everything French, the beach at sunrise, my three cats, and hope to revisit the city of San Francisco one day.

Reader Interactions

Comment Policy: Your words are your own, so be nice and helpful if you can. Please, only use your real name and limit the amount of links submitted in your comment. We accept clean XHTML in comments, but don't overdo it please. Comments are moderated, so spammy or malicious links will be removed.