Career Pivot into Development Journal, Day 4: JS Practice 2 – repeatString.js
Tags: beginners, webdev, codenewbie, devjournal
Published: 2022-03-22 02:16:29

**_Just finished Fundamentals 4 of The Odin Project...now onto the practice section. _**

[repeatString assignment link](https://github.com/TakDevelops/javascript-exercises/tree/main/02_repeatString)

---

##Understanding the Problem
Write a function that repeats a string a given number of times, both parameters inputted by the user. In other words, the string will be repeated, or concatenated, next to each other `num` number of times

---

##Plan
- Does program have a UI? What will it look like? What functionality will the interface have? Sketch this out on paper. **_No UI_**
- What are the inputs? Will the user enter data or will you get input from somewhere else?
    **_- string = any string that a user inputs when prompted_**
    **_- num = any number the user inputs when prompted_**
- What’s the desired output? **_A string that concatenates itself n number of times_**

---

##Pseudocode
```
Prompt the user to input a string and store this value in the variable `string`

Prompt the user to input a number and store this value in the variable `num`

Concatenate the `string` `num` number of times by looping `num` number of times, and save it to a variable

return the concatenated string
```

---

##Divide and Conquer
Let's start with the most straight forward subproblems first...

###Prompt the user to input a string and store this value in the variable `string`
```
string = prompt('Enter a string');
```

###Prompt the user to input a number and store this value in the variable `num`
```
num = prompt('Enter a number');
```

**_Now let's figure out the more complex algorithm_**

###Concatenate the `string` `num` number of times by looping `num` number of times, and save it to a variable

Let's create a loop first
```
for (let i = 0; i < num; i++) {
    
}
```

We'll declare a variable to store the concatenated string, then concatenate this with the user's input `string`
```
for (let i = 0; i < num; i++) {
    let concatenatedString = '';
    concatenatedString += string;
}
```
The value of `concatenatedString` should initially be empty because on the first loop, we want `concatenatedString` to only return one `string`.

###return the concatenated string
```
for (let i = 0; i < num; i++) {
    let concatenatedString = '';
    concatenatedString += string;
}
    return concatenatedString;
```
We want the final value of concatenatedString to be returned at the end of the loop.

---

##Putting it all together
```
const repeatString = function(string, num) {
    let concatenatedString = '';
    for (i = 0; i < num; i++) {
        concatenatedString += string;
    }
    return concatenatedString;
};

string = prompt('Enter one word below');
num = prompt('Enter a number below');

repeatString(string, num);
```

---

##Explaining the algorithm in plain english
We declared a function repeatString initially that takes the parameters string and num.

Inside this function, we declare a variable called concatenatedString. It's initial value is set as an empty string

Then a loop loops num number of times and concatenates concatenatedString to string. In the first loop since concatenatedString is initially empty, an empty string '' is concatenated with the string input

So let's assume the string parameter = hey

In the first loop, concatenatedString = '' + hey which = hey.

In the second loop, the expression concatenatedString += string equals concatenatedString = hey + hey

Etc etc. When the loop ends the final value of concatenatedString is returned