Skip to content

Latest commit

 

History

History
87 lines (67 loc) · 2.7 KB

Bonfire-Repeat-a-String-Repeat-a-String.md

File metadata and controls

87 lines (67 loc) · 2.7 KB

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.

Hint: 1

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

Hint: 2

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

Hint: 3

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

Spoiler Alert!

687474703a2f2f7777772e796f75726472756d2e636f6d2f796f75726472756d2f696d616765732f323030372f31302f31302f7265645f7761726e696e675f7369676e5f322e676966.gif

Solution ahead!

Code Solution:

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

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

  return accumulatedStr;
}

Second Solution:

function repeat(str, num) {
  var newstr = [];
  for (var i = 0; i < num; i++) {
    newstr.push(str);
  }
  return newstr.join('');
}

repeat("abc", 3);

Code Explanation:

  • Create a variable to store the repeated word.
  • Use a while loop or for loop to repeat code as many times as needed according to num
  • The 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.

Third Solution:

function repeat(str, num) {
  if (num < 0) {
    return "";
  } 
  else {
    return str.repeat(num);
  }
}

repeat("abc", 3);

Code Explanation:

  • First check if num is a negative number and return false if so
  • as of ECMA Script 6 (ES6) the String object comes with a builtin function to repeat a string which we you can use

##Recursive Solution

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

Credits:

If you found this page useful, you can give thanks by copying and pasting this on the main chat: Thanks @Rafase282 @shadowfool for your help with Bonfire: Repeat a String Repeat a String

NOTE: Please add your username only if you have added any relevant main contents to the wiki page. (Please don't remove any existing usernames.)