function my_repeat(str, n) {
let result = ''
while(n > 0) {
result += str
n--
}
return result
}
function my_repeat(str, n, init) {
init = init ? init : str
if(n === 0) return str
str += init
return my_repeat2(str, n - 1, init)
}
let num = 0
function my_repeat3(str, n, defaultStr) {
if(!defaultStr) defaultStr = str
if(n === 1) return str
if(n === 2) return str + str
let hashMap = Object.create(null)
while(str.length <= Math.floor(n / 2)) {
hashMap[str.length] = str
str += str
num++
}
let newStr = ''
Object.keys(hashMap).forEach(k => {
newStr += hashMap[k]
num++
})
return newStr + my_repeat(defaultStr, n - newStr.length)
}
console.log(my_repeat('a', 1023, undefined))
console.log(num) // 36
网友评论