loading...

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

virenb profile image Viren B Updated on ・2 min read

Solving freeCodeCamp's Algorithm Challenges (37 Part Series)

1) FCC Algorithm Challenges / Convert Celsius to Fahrenheit 2) FCC Algorithm Challenges / Reverse a String 3 ... 35 3) FCC Algorithm Challenges / Factorialize a Number 4) FCC Algorithm Challenges / Find the Longest Word in a String 5) FCC Algorithm Challenges / Return Largest Numbers in Arrays 6) Solving "Confirm the Ending" / FreeCodeCamp Algorithm Challenges 7) Solving "Repeat a String Repeat a String" / FreeCodeCamp Algorithm Challenges 8) Solving "Truncate a String" / freeCodeCamp Algorithm Challenges 9) Solving "Finders Keepers" / freeCodeCamp Algorithm Challenges 10) Solving "Boo who" / freeCodeCamp Algorithm Challenges 11) Solving "Title Case a Sentence" / freeCodeCamp Algorithm Challenges 12) Solving "Slice and Splice" / freeCodeCamp Algorithm Challenges 13) Solving "Falsy Bouncer" / freeCodeCamp Algorithm Challenges 14) Solving "Where do I Belong" / freeCodeCamp Algorithm Challenges 15) Solving "Mutations" / freeCodeCamp Algorithm Challenges 16) Solving "Chunky Monkey" / freeCodeCamp Algorithm Challenges 17) Solving "Sum All Numbers in a Range" / freeCodeCamp Algorithm Challenges 18) Solving "Diff Two Arrays" / freeCodeCamp Algorithm Challenges 19) Solving "Seek and Destroy" / freeCodeCamp Algorithm Challenges 20) Solving "Wherefore art thou" / freeCodeCamp Algorithm Challenges 21) Solving "Spinal Tap Case" / freeCodeCamp Algorithm Challenges 22) Solving "Pig Latin" / freeCodeCamp Algorithm Challenges 23) Solving "Search and Replace" / freeCodeCamp Algorithm Challenges 24) Solving "DNA Pairing" / freeCodeCamp Algorithm Challenges 25) Solving "Missing letters" / freeCodeCamp Algorithm Challenges 26) Solving "Sorted Union" / freeCodeCamp Algorithm Challenges 27) Solving "Convert HTML Entities" / freeCodeCamp Algorithm Challenges 28) Solving "Sum All Odd Fibonacci Numbers" / freeCodeCamp Algorithm Challenges 29) Solving "Sum All Primes" / freeCodeCamp Algorithm Challenges 30) Solving "Smallest Common Multiple" / freeCodeCamp Algorithm Challenges 31) Solving "Drop it" / freeCodeCamp Algorithm Challenges 32) Solving "Steamroller" / freeCodeCamp Algorithm Challenges 33) Solving "Binary Agents" / freeCodeCamp Algorithm Challenges 34) Solving "Everything Be True" / freeCodeCamp Algorithm Challenges 35) Solving "Arguments Optional" / freeCodeCamp Algorithm Challenges 36) Solving "Make a Person" / freeCodeCamp Algorithm Challenges 37) Solving "Map the Debris" / freeCodeCamp Algorithm Challenges

Original post can also be found on my website, https://virenb.cc/fcc-007-repeat-a-string

"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]

    function repeatStringNumTimes(str, num) {
        let newStr = "";

        if (num &lt;= 0) return "";

        for (let i = 0; i &lt; num; i++) {
            newStr += str;
        }

        return newStr;
    }

Links & Resources

Repeat a String Repeat a String Challenge on FCC

FreeCodeCamp

Donate to FCC!

Solution on my GitHub

Thank you for reading!

Solving freeCodeCamp's Algorithm Challenges (37 Part Series)

1) FCC Algorithm Challenges / Convert Celsius to Fahrenheit 2) FCC Algorithm Challenges / Reverse a String 3 ... 35 3) FCC Algorithm Challenges / Factorialize a Number 4) FCC Algorithm Challenges / Find the Longest Word in a String 5) FCC Algorithm Challenges / Return Largest Numbers in Arrays 6) Solving "Confirm the Ending" / FreeCodeCamp Algorithm Challenges 7) Solving "Repeat a String Repeat a String" / FreeCodeCamp Algorithm Challenges 8) Solving "Truncate a String" / freeCodeCamp Algorithm Challenges 9) Solving "Finders Keepers" / freeCodeCamp Algorithm Challenges 10) Solving "Boo who" / freeCodeCamp Algorithm Challenges 11) Solving "Title Case a Sentence" / freeCodeCamp Algorithm Challenges 12) Solving "Slice and Splice" / freeCodeCamp Algorithm Challenges 13) Solving "Falsy Bouncer" / freeCodeCamp Algorithm Challenges 14) Solving "Where do I Belong" / freeCodeCamp Algorithm Challenges 15) Solving "Mutations" / freeCodeCamp Algorithm Challenges 16) Solving "Chunky Monkey" / freeCodeCamp Algorithm Challenges 17) Solving "Sum All Numbers in a Range" / freeCodeCamp Algorithm Challenges 18) Solving "Diff Two Arrays" / freeCodeCamp Algorithm Challenges 19) Solving "Seek and Destroy" / freeCodeCamp Algorithm Challenges 20) Solving "Wherefore art thou" / freeCodeCamp Algorithm Challenges 21) Solving "Spinal Tap Case" / freeCodeCamp Algorithm Challenges 22) Solving "Pig Latin" / freeCodeCamp Algorithm Challenges 23) Solving "Search and Replace" / freeCodeCamp Algorithm Challenges 24) Solving "DNA Pairing" / freeCodeCamp Algorithm Challenges 25) Solving "Missing letters" / freeCodeCamp Algorithm Challenges 26) Solving "Sorted Union" / freeCodeCamp Algorithm Challenges 27) Solving "Convert HTML Entities" / freeCodeCamp Algorithm Challenges 28) Solving "Sum All Odd Fibonacci Numbers" / freeCodeCamp Algorithm Challenges 29) Solving "Sum All Primes" / freeCodeCamp Algorithm Challenges 30) Solving "Smallest Common Multiple" / freeCodeCamp Algorithm Challenges 31) Solving "Drop it" / freeCodeCamp Algorithm Challenges 32) Solving "Steamroller" / freeCodeCamp Algorithm Challenges 33) Solving "Binary Agents" / freeCodeCamp Algorithm Challenges 34) Solving "Everything Be True" / freeCodeCamp Algorithm Challenges 35) Solving "Arguments Optional" / freeCodeCamp Algorithm Challenges 36) Solving "Make a Person" / freeCodeCamp Algorithm Challenges 37) Solving "Map the Debris" / freeCodeCamp Algorithm Challenges

Posted on by:

virenb profile

Viren B

@virenb

Self-taught developer, always looking to learn more.

Discussion

markdown guide