1

I did a very thorough search before asking this, but I couldn't find exactly what I was looking for. I want to preface that this is my fifth day coding in JavaScript, so be easy on me, please!

I'm taking a preparatory coding course for a full-fledged program and one of our objectives was to write a function repeatString(string, count) that takes a string and the number of times you want it repeated as parameters. Here is my version with recursion:

function repeatString(string, count) {
    if ( count === 0 ) {
        return "";
    }
    if ( count === 1 ) {
        return string;
    }
    if ( count > 1 ) {
    return string + repeatString(string, count - 1); 
    }
}

We were then supposed to rewrite the function using while instead of recursion. This is where I got stuck:

function repeatString(string, count) {

    var num = 0;

    if ( count === 0 ) {
        return "";
    }
    while ( num < count ) {
        num += 1;
        return string;
    }
}

My current code returns a string only once no matter the count, unless the count is zero. I'm sure there is a glaring error right in front of me but my beginner eyes are not catching it.

nnnnnn
  • 147,572
  • 30
  • 200
  • 241
vaponteblizzard
  • 186
  • 2
  • 12
  • For future reference `return` will exit the function as soon as it is called, so your while loop will only make it through one iteration before breaking. This comes in very handy in a lot of situations but in this case you should so as @mikeb is suggesting. – thodic May 14 '16 at 00:50
  • @OhAuth That is incredibly useful to know, thanks! – vaponteblizzard May 14 '16 at 01:03

5 Answers5

2

Try this, you need to build the string before you return it. If you're not using recursion, you only return once:

function repeatString(string, count) {

  var num = 0;
  var str = "";
  if ( count === 0 ) {
      return "";
  }
  while ( num < count ) {
      num += 1;
      str = str + string;
  }
  return str;
}

repeatString("asdf", 2)

"asdfasdf"

mikeb
  • 10,578
  • 7
  • 62
  • 120
1

here an easy one:

function repeatString(string, count) {
    return new Array(count + 1).join(string);
}

repeatString('asd', 2) // "asdasd"
webdeb
  • 12,993
  • 5
  • 28
  • 44
0

How can I repeat strings in JavaScript? provides two elegant solutions, both based on initializing an array of the needed size, then joining its items to form the resulting string.

If using while is a must, then you still can make make the function shorter. Note, using reserved words to name your variables is poor style, so I replaced "string" with "s", and "str" with "result" for clarity.

function repeatString(s, count) {
  var result = "";
  var num = 1;
  while ( num++ <= count ) {
      result += s;
  }
  return result;
}
Community
  • 1
  • 1
0

Here is one too, with es6

var repeater = (str) => {
       return (times) => {
            return [...Array(times)].reduce((p,c) => {
              return p.concat(str)
            }, []).join('')
       }

}

repeater('yoyo')(10)

or

  var R = repeater('test')
      R(5)
james emanon
  • 11,185
  • 11
  • 56
  • 97
0

Shortest answer:

function repeatString(str,num){
   return str.repeat(num);
}
repeatString('hello', 2);
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135