Repeat a String Repeat a String

Learn how to solve the freeCodeCamp algorithm 'Repeat a String Repeat a String' using a for-loop, Array.push(), and Array.join().

Repeat a String Repeat a String

In this freeCodeCamp algorithm, we need to come up with a way to repeat a given string a specified amount of times.

For Example
If were given the string "code" and we are to repeat it 5 times, the result should return "codecodecodecodecode".

If number of times to repeat string is not a positive number we will return an empty string.

Spec

  • repeatStringNumTimes("", 3) should return "**"
  • repeatStringNumTimes("abc", 3) should return "abcabcabc"
  • repeatStringNumTimes("abc", 4) should return "abcabcabcabc"
  • repeatStringNumTimes("abc", 1) should return "abc"
  • repeatStringNumTimes("*", 8) should return "********"
  • repeatStringNumTimes("abc", -2) should return ""
  • The built-in repeat() method should not be used

Psuedocode

Given the specification of the problem, we can use psuedocode to see how we can approach this. Here's what I came up with:

// Create a function that accepts two arguments, str and num
    
    // Create an array to store the repeated str
    
    // Create a check for when num is not a positive number
    
    // Create a loop that pushes str to array num times
    
    // Join the repeated str and return it

Solving the Algorithm

Setting up the function

We can use the basic boilerplate function from freeCodeCamp to start:

function repeatStringNumTimes(str, num) {
  // repeat after me
  return str;
}

repeatStringNumTimes("abc", 3);

Adding array to store the repeated string

My thinking with this was that we can create a loop that will repeat str num times, and every time we repeat str, we can just store it in this array. Once we're done we can return all of the values of the array as a single string.

function repeatStringNumTimes(str, num) {
  // repeat after me
  const repeatedStr = [];
  
  return str;
}

repeatStringNumTimes("abc", 3);

Check if num is not positive

According to the specification, if num is not positive then we must return an empty string.

function repeatStringNumTimes(str, num) {
  // repeat after me
  const repeatedStr = [];
  
  if (num < 1) {
    return "";
  }
  
  return str;
}

repeatStringNumTimes("abc", 3);

Repeat str

Here's where the bulk of the logic will come from. We can create a for-loop that will repeat str however many times that num specifies.

Once we repeat str, we can use the Array.push() method to add it to our repeatedStr array.

function repeatStringNumTimes(str, num) {
  // repeat after me

  var repeatedStr = [];

  if(num < 1) {
    return "";
  }

  for(var i = 0; i < num; i++) {
    repeatedStr.push(str);
  }
}

repeatStringNumTimes("abc", 3);

Join the repeatedStr array and return result

Now that we have str being repeated num times and being added to repeatedStr we need to find a way to convert repeatedStr into one big string and get rid of the commas.

For example
If we ran our function as repeatStringNumTimes("code", "5"); and were to look at repeatedStr it would look something like this: ["code", "code", "code", "code", "code"]. However, we want to make sure it's returned as "codecodecodecodecode", which is a String, and not an Array

Lucky for us, there's a handy method called Array.join() which we can use to convert all of the items in an array to a single string.

From the specification of Array.join(), if we are to pass in an empty string as the separator parameter then it will join all of the items together without spaces or commas. This is exactly what we want!

function repeatStringNumTimes(str, num) {
  // repeat after me

  var repeatedStr = [];

  if(num < 1) {
    return "";
  }

  for(var i = 0; i < num; i++) {
    repeatedStr.push(str);
  }

  return repeatedStr.join('');
}

repeatStringNumTimes("abc", 3);

Try this the code snippet in your browser console.

Recap

We were able to solve this freeCodeCamp algorithm by breaking down the requirements and using a for-loop, the Array.push() method, and the Array.join() method. Hopefully you found this walk through helpful and if you have a solution of your own I'd love to see it!

Shoot me an email at [email protected] with any questions and if you enjoyed this, stay tuned and subscribe below! 👇

Help us improve our content