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.
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.
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.
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.
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.
Lastly, we return the variable with the repeated string.
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!
Top comments (8)
Math.sign(-1) is -1, which is true, so your example code will accept negative numbers, and only reject 0.
What you have written is equivalent to if (num !== 0) { ... }
I recommend writing if (num > 0) { ... } given what you've written in the comments.
If you're doing this, you could just replace the whole thing with a simple recursive expression.
But using + iteratively on strings produces a lot of intermediary junk, so I'd suggest using join instead.
But the simplest approach is probably this. :)
Thanks for sharing your solutions! I love seeing what other people come up with because it makes me a better programmer. My code passed the tests on FCC, and I ran it myself with both zero and a negative number. In both instances I got back an empty string.
That's because it never gets to the if (Math.sign(num)) bit in your example.
Initially i === 0, so the guard is initially 0 < num, which will be false if num <= 0.
But you can easily test the behavior of Math.sign.
"Repeat a given string str (first argument) for num times (second argument). Return an empty string if num is not a positive number."
dev-to-uploads.s3.amazonaws.com/i/...
dev-to-uploads.s3.amazonaws.com/i/...
dev-to-uploads.s3.amazonaws.com/i/...
In what case do you think you call Math.sign with a value less than one?
I'm using Math.sign() to check whether or not the number is positive or negative. If it never gets to my if statement, then how is it updating the variable and returning the string the correct number of times?
I guess I only call Math.sign for the positive case, the else statement returns the empty string for the other cases. I still don't see how this isn't a correct solution to the problem. I check to see if it is a positive number with Math.sign, if it is the variable gets updated and returned the correct number of times. In all other cases, negative or zero, it returns an empty string.
So, if (Math.sign(num)) { . . . } could be replaced with if (true) { ... } in your code without changing any answer?