Viren Bhagat

Solving "Repeat a String Repeat a String" / FreeCodeCamp Algorithm Challenges

5/18/2020

"Repeat a String Repeat a String" Challenge

Let's solve freeCodeCamp's Basic Algorithm Scripting Challenge, "Repeat a String Repeat a String"

Our Starter Code (& Tests)

function repeatStringNumTimes(str, num) {
  return str;
}

repeatStringNumTimes("abc", 3);
// Tests
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.
repeatStringNumTimes("abc", 0) should return "".

Our Instructions

Repeat a given string str (first argument) for num times (second argument). Return an empty string if num is not a positive number.


Thoughts

  • We have two inputs, one being a str being a string, num being a number
  • We have to return a string value
  • Do not use .repeat(), a built-in method
  • Need to check the value of num. If it is not a positive number, we must return an empty string

Further Thoughts

There are definitely a few ways to solve this

Usually, I tend to try with a for loop first. Since one of the arguments is a number, we can run the for loop that amount of times

One of the instructions mentioned to return an empty string if num was not a positive number, so we can do a quick if statement to check and return an empty string

We can declare an empty string variable, create our for loop, run it num times, and add the str to the new empty string variable, newStr

Then ensure to return newStr

Let's look at that with some pseudo pseudocode

function repeatStringNumTimes(str, num) {
    if num <= 0 
        return ""
        
    create newStr variable, set to empty string 
    for loop: i = 0; i < num; i++
        Add str value to newStr each loop
        
    return newStr   
}

repeatStringNumTimes("abc", 3);

So the above code would run something like this:

repeatStringNumTimes("abc", 3);

if (num <= 0) return "" 
// would return false and function would continue running since num is 3

let newStr = "" 
// new empty string variable set

for (let i = 0; i < num; i++) {
    newStr += str;
}
// Will loop 3 times since num is 3
// First loop - newStr == "abc"
// Second loop - newStr == "abcabc"
// Third loop - newStr == "abcabcabc"

return newStr; // "abcabcabc"

Solution

**[SPOILER: SOLUTION TO CODE BELOW]** ```javascript function repeatStringNumTimes(str, num) { let newStr = "";
    if (num <= 0) return "";

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


</details>

---

### Links & Resources

[Repeat a String Repeat a String Challenge on FCC](https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-algorithm-scripting/repeat-a-string-repeat-a-string "Basic Algorithm Scripting: Repeat a String Repeat a String Challenge on FCC Website")

[FreeCodeCamp](https://www.freecodecamp.org/, "FreeCodeCamp")

[Donate to FCC!](https://www.freecodecamp.org/donate "FCC Donation Page")

[Solution on my GitHub](https://github.com/virenb/fcc-algorithms/blob/master/007---repeat-a-string.js "GitHub Solution")

Thank you for reading! 
fcc, algorithms, javascript