Solving "Repeat a String Repeat a String" / FreeCodeCamp Algorithm Challenges 
Tags: fcc, algorithms, challenge, javascript
Published: 2020-05-18 19:08:15

---
title: Solving "Repeat a String Repeat a String" / FreeCodeCamp Algorithm Challenges 
published: true
description: My guide, notes, and solution to FreeCodeCamp's basic algorithm challenge, "Repeat a String Repeat a String"
tags: #fcc, #algorithms, #challenge, #javascript
canonical_url: https://virenb.cc/fcc-007-repeat-a-string
series: Solving freeCodeCamp's Algorithm Challenges
---

Original post can also be found on my website, [https://virenb.cc/fcc-007-repeat-a-string] (https://virenb.cc/fcc-007-repeat-a-string "Post on my website")

!["Repeat a String Repeat a String" Challenge](//images.ctfassets.net/yzwpq1epaq68/1t90aNn1jPPVX70lvYszZN/bc5db97aff58d83e07f8a1bea008972d/ss-007.png)

Let's solve freeCodeCamp's Basic Algorithm Scripting Challenge, "Repeat a String Repeat a String"

## Our Starter Code (& Tests)
```javascript
function repeatStringNumTimes(str, num) {
  return str;
}

repeatStringNumTimes("abc", 3);
```
```
// Tests
repeatStringNumTimes("*", 3) should return "***".
repeatStringNumTimes("abc", 3) should return "abcabcabc".
repeatStringNumTimes("abc", 4) should return "abcabcabcabc".
repeatStringNumTimes("abc", 1) should return "abc".
repeatStringNumTimes("*", 8) should return "********".
repeatStringNumTimes("abc", -2) should return "".
The built-in repeat() method should not be used.
repeatStringNumTimes("abc", 0) should return "".
```

### Our Instructions


Repeat a given string `str` (first argument) for `num` times (second argument). Return an empty string if `num` is not a positive number.

---

## Thoughts

* We have two inputs, one being a `str` being a string, `num` being a number
* We have to return a string value
* Do not use `.repeat()`, a built-in method
* Need to check the value of `num`. If it is not a positive number, we must return an empty string


### Further Thoughts

There are definitely a few ways to solve this

Usually, I tend to try with a for loop first. Since one of the arguments is a number, we can run the for loop that amount of times

One of the instructions mentioned to return an empty string if `num` was not a positive number, so we can do a quick `if` statement to check and return an empty string

We can declare an empty string variable, create our for loop, run it `num` times, and add the `str` to the new empty string variable, `newStr`

Then ensure to return `newStr`

Let's look at that with some pseudo pseudocode 

```javascript
function repeatStringNumTimes(str, num) {
	if num <= 0 
		return ""
		
	create newStr variable, set to empty string	
	for loop: i = 0; i < num; i++
		Add str value to newStr each loop
		
	return newStr	
}

repeatStringNumTimes("abc", 3);

``` 

So the above code would run something like this:

```
repeatStringNumTimes("abc", 3);

if (num <= 0) return "" 
// would return false and function would continue running since num is 3

let newStr = "" 
// new empty string variable set

for (let i = 0; i < num; i++) {
	newStr += str;
}
// Will loop 3 times since num is 3
// First loop - newStr == "abc"
// Second loop - newStr == "abcabc"
// Third loop - newStr == "abcabcabc"

return newStr; // "abcabcabc"
```

---

## Solution

<details>
  <summary>**[SPOILER: SOLUTION TO CODE BELOW]**</summary>
 	
  ```javascript
	function repeatStringNumTimes(str, num) {
  		let newStr = "";

		if (num <= 0) return "";
  		
  		for (let i = 0; i < num; i++) {
    		newStr += str;
  		}
  		
  		return newStr;
	}
  ```
 
</details>

---

### Links & Resources

[Repeat a String Repeat a String Challenge on FCC](https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-algorithm-scripting/repeat-a-string-repeat-a-string "Basic Algorithm Scripting: Repeat a String Repeat a String Challenge on FCC Website")

[FreeCodeCamp](https://www.freecodecamp.org/, "freeCodeCamp")

[Donate to FCC!](https://www.freecodecamp.org/donate "FCC Donation Page")

[Solution on my GitHub](https://github.com/virenb/fcc-algorithms/blob/master/007---repeat-a-string.js "GitHub Solution")

Thank you for reading!