```js
function repeatString(str, n) {
  for (var r = ''; n > 0; (n >>= 1) && (str += str))
    if (n & 1) r += str;
  return r;
}
```