Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save SerhiiLihus/6183ebf92dbd5fe86fb5 to your computer and use it in GitHub Desktop.
Save SerhiiLihus/6183ebf92dbd5fe86fb5 to your computer and use it in GitHub Desktop.
Repeat a string repeat a string
// Bonfire: Repeat a string repeat a string
// Author: @serhiilihus
// http://www.freecodecamp.com/challenges/bonfire-repeat-a-string-repeat-a-string?solution=function%20repeat%28str%2C%20num%29%20{%0A%20%20var%20newString%20%3D%20%22%22%3B%0A%20%20if%20%28%20num%20%3E%200%20%29%20{%0A%20%20for%20%28var%20i%20%3D%200%3B%20i%20%3C%20num%20%3B%20i%2B%2B%20%29%0A%20%20%20%20%20%20newString%20%2B%3D%20str%3B%0A%20%20return%20newString%3B%0A%20%20}%20else%20return%20%22%22%3B%0A}%0A%0Arepeat%28%22abc%22%2C%203%29%3B%0A
// Learn to Code at Free Code Camp (www.freecodecamp.com)
function repeat(str, num) {
var newString = "";
if ( num > 0 ) {
for (var i = 0; i < num ; i++ )
newString += str;
return newString;
} else return "";
}
@SergeyPodgornyy
Copy link

Мне кажется, что я проще реализовал

function repeat(str, num) {
  // repeat after me
  var strinG = "";

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

  return strinG;
}

repeat("abc", 3);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment