Basic Algorithm Scripting in JavaScript
Tags: javascript, algorithms, scripting, practice
Published: 2020-08-30 22:42:44

Lately I have been working my way through Free Code Camp's, JavaScript Algorithms and Data Structures certification lessons. I am presently on the Basic Algorithm Scripting section, and I thought I would share one of my solutions to a problem I recently solved. 

This is my solution to the the "Reapeat a String" problem which states, "Repeat a given string str (first argument) for num times (second argument). Return an empty string if num is not a positive number." Whenever I begin to solve a coding challenge I always like to re-word the problem in plain language that is easier for me to understand. So, this problem is essentially asking us to write a function that takes in two arguments, the first will be a string, and the second will be a number. We are then supposed to figure out a way for our function to repeat the string based on whatever the initial number is that we pass to our function. If we pass in the word "coffee," and the number 5, our function should return the word "coffee" five times like this, "coffeecoffeecoffee coffeecoffee." There is also another condition that states that if a negative number is passed into our function, the function should instead return an empty string.

Now that we clearly understand what we are being asked to accomplish with our function we can get to work solving it. I like to write out a skeleton function first like this. 
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/1qtdvvpj81pq6lqzt53e.png)
We know our function takes in a string and number. We also know that it will return a string.

Since we are working with a string manipulation problem, our function will need a variable to store the string. 
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/03o25chlwovroweyb0yl.png)

Now since we need to do something a specific number of times, we should definitely use some kind of loop for this problem. There are always more than one way to solve a problem, especially with JavaScript, but I'm going to use the tried and true for loop. 
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/06tot8il24jovctovqn6.png)
Using the for loop gives us a way to add to and update our string as many times as we need, based on the value of the number we pass in. 

Our problem also gives us a condition that we must check. We only update our variable if the number we pass in is a positive integer. JavaScript, being the awesome language that it is, gives us a nifty way to check if a number is positive or negative with the Math.sign() function. Math.sign() is a builtin function in JavaScript and is used to know the sign of a number, indicating whether the number specified is negative or positive. 
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/ejaciv44skph0gtqz77n.png)
So with a simple conditional statement, we can check the sign of the number being passed in, and if that number is positive, we can update the string inside the variable inside our function. 

Also, if the number being passed into our function is not positive i.e. zero or a negative number, we are to return an empty string. 
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/kb0wy5vh1i00y13g42lh.png)
Lastly, we return the variable with the repeated string.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/fhle35cd6yavoo8mn2w7.png)

I hope this breakdown of a a JS algorithm scripting problem has been helpful. I have found that solving a lot of these types of problems on a daily basis has greatly improved my ability to solve more difficult problems on sites like Code Wars, HackerRank, and CoderByte.

Thanks for reading and happy coding!