DEV Community

TakDevelops
TakDevelops

Posted on • Updated on

Career Pivot into Development Journal, Day 4: JS Practice 2 – repeatString.js

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

repeatString assignment link


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
Enter fullscreen mode Exit fullscreen mode

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');
Enter fullscreen mode Exit fullscreen mode

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

num = prompt('Enter a number');
Enter fullscreen mode Exit fullscreen mode

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++) {

}
Enter fullscreen mode Exit fullscreen mode

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;
}
Enter fullscreen mode Exit fullscreen mode

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;
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

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

Top comments (0)