DEV Community

Mohammed Awad
Mohammed Awad

Posted on

Repeat a String for Num times

DESCRIPTION:

Repeat a given string str (first argument) for num times (second argument). Return an empty string if num is not a positive number. For the purpose of this challenge, do not use the built-in .repeat() method.

Examples

repeatStringNumTimes("abc", 3)
//should return `abcabcabc`.
Enter fullscreen mode Exit fullscreen mode

My approach for solving this problem:

  • return "" if the num is negative.
  • loop for num times
  • storge the str value inside result var
  • return the final result

My solution:

function repeatStringNumTimes(str, num) {
  let result = ""

  if(num<0){
    return ""
  }

  for(let i =0;i<num;i++){
    result += str
  }

  return result;
}

repeatStringNumTimes("abc", 3);
Enter fullscreen mode Exit fullscreen mode

I breath with your support, sometimes I feel discouraged when I don't get reactions on my posts. so please Like đź‘Ť & share if you can. Thanks for being here!

Follow Muhmmad Awd on

If you have any questions or feedback, please feel free to contact me at

Top comments (3)

Collapse
 
kalkwst profile image
Kostas Kalafatis

Hello there,

Thank you for sharing your answer to the "repeat a string" problem! Your strategy is clear and succinct, and your code is simple to read and comprehend.

However, I noticed a few potential concerns that you should think about:

Your existing solution has a limitation in terms of error handling. While it produces an empty string when the specified value for num is negative, it fails to address other scenarios where num is either not a number or zero.

If the function is invoked with repeatStringNumTimes("abc", "not a number"), for example, it will behave unexpectedly rather than producing an error or returning a meaningful warning to the user. Similarly, calling the function with repeatStringNumTimes("abc", 0) will result in an empty string, which may not be the desired behavior.

You may want to consider adding some more error handling to your solution to make it more robust and user-friendly.

On the performance front, concatenating strings in a loop can be computationally expensive for high values of num, because each concatenation generates a new string object that must be held in memory. This might cause performance concerns, especially for applications that must analyze massive amounts of data.

To improve performance, one way is to use an array to hold the repeated string values and then connect them at the end. This can be accomplished by first creating an array with num copies of the original string and then concatenating the array with the Array.join() method.

Another method is to split the num value in half and recursively concatenate the string until the desired number of repeats is attained. Because it avoids the creation of intermediary strings, this approach may be more memory-efficient than the loop approach. However, it may be less obvious and more difficult to read and debug.

Finally, in your existing solution, you have a condition that checks num and returns an empty string if it is less than 0. This is used to handle negative num numbers. For positive num numbers, however, there is a loop that iterates num times to concatenate the str value to result. This results in code duplication, when the same logic is repeated twice in your code.

To prevent this redundancy, you might check if num is negative using an if-else expression. You can return an empty string if it is negative. Otherwise, continue the loop to concatenate the str value to result. This eliminates the need to repeat code, making your solution more concise and easier to read.

Overall, your solution is a good start, and with a few improvements, it could be even better. Keep up the good work!

Collapse
 
xmohammedawad profile image
Mohammed Awad

I would like to hear any notes or comments to help improve this article.

Collapse
 
siddharthshyniben profile image
Siddharth • Edited

This function works great, but in the real world, you're better off using the builtin String.prototype.repeat function.

"na ".repeat(10) // => "na na na na na na na na na na " // batman!
Enter fullscreen mode Exit fullscreen mode