Algorithm Repeat A String Repeat A String - ashish9342/FreeCodeCamp GitHub Wiki

Algorithm Repeat a String

🚩 Remember to use Read-Search-Ask if you get stuck. Try to pair program 👥 and write your own code 📝

🏁 Problem Explanation:

The program is very simple, we have to take a variable and return that variable being repeated certain amount of times. No need to add space or anything, just keep repeating it into one single string.

Relevant Links

💬 Hint: 1

You can't edit strings, you will need to create a variable to store the new string.

try to solve the problem now

💬 Hint: 2

Create a loop to repeat the code as many times as needed.

try to solve the problem now

💬 Hint: 3

Make the variable created store the current value and append the word to it.

try to solve the problem now

Spoiler Alert!

687474703a2f2f7777772e796f75726472756d2e636f6d2f796f75726472756d2f696d616765732f323030372f31302f31302f7265645f7761726e696e675f7369676e5f322e676966.gif

Solution ahead!

🔰 Basic Code Solution:

function repeatStringNumTimes(str, num) {
  var accumulatedStr = '';

  while (num > 0) {
    accumulatedStr += str;
    num--;
  }

  return accumulatedStr;
}

🚀 Run Code

Code Explanation:

  • Create an empty string variable to store the repeated word.
  • Use a while loop or for loop to repeat code as many times as needed according to num
  • Then we just have to add the string to the variable created on step one, and increase or decrease num depending on how you set the loop.
  • At the end of the loop, return the variable for the repeated word.

Relevant Links

🌻 Intermediate Code Solution:

function repeatStringNumTimes(str, num) {
  if(num < 0)
    return "";
  if(num === 1)
    return str;
  else
    return str + repeatStringNumTimes(str, num - 1);
}

🚀 Run Code

Code Explanation:

  • This solution uses recursion.
  • We check if num is negative and return an empty string if true.
  • Then we check if it's equal to 1 and in that case we return the string itself.
  • If not, we add the string to a call of our function with num being decreased by 1, which will add another str and another.. until eventually num is 1. And return that whole process.

Relevant Links

🚨 Advanced Code Solution:

function repeatStringNumTimes(str, num) {
  return num > 0 ? str.repeat(num) : '';
}

repeatStringNumTimes("abc", 3);

🚀 Run Code

Code Explanation:

  • This solution takes a declarative approach.
  • It is similar to the third solution, except it uses the ternary operator form of the if statement.

Relevant Links

🏆 Credits:

If you found this page useful, you may say thanks to the contributors by copying and pasting the following line in the main chat:

Thanks @Rafase282 @shadowfool @Hallaathrad @sgalizia for your help with Algorithm: Repeat a String Repeat a String

📋 NOTES FOR CONTRIBUTIONS:

  • ⚠️ DO NOT add solutions that are similar to any existing solutions. If you think it is similar but better, then try to merge (or replace) the existing similar solution.
  • Add an explanation of your solution.
  • Categorize the solution in one of the following categories — Basic, Intermediate and Advanced. 🚥
  • Please add your username only if you have added any relevant main contents. (:warning: DO NOT remove any existing usernames)

See 👉 Wiki Challenge Solution Template for reference.

⚠️ **GitHub.com Fallback** ⚠️