给出字符串和重复次数,返回重复屡次的字符串code
repeatStringNumTimes("abc", 3) repeatStringNumTimes("abc", -2) should return "".
while循环递归
num控制循环次数ip
function repeatStringNumTimes(str,num) { var newstr = ''; while(num>0) { newstr += str; num--; } return newstr; } repeatStringNumTimes("abc", 3);
str.repeat()方法字符串
//写法1 function repeatStringNumTimes(str,num) { if(num>0) { return str.repeat(num); } return ""; } //写法2 function repeatStringNumTimes(str,num) { return num > 0 ? str.repeat(num) : ""; } repeatStringNumTimes("abc", 3);
if语句get
递归io
function repeatStringNumTimes(str,num) { if(num<0) { return ""; } else if(num=0|1) { return str } else { return str + repeatStringNumTimes(str,num-1); } } repeatStringNumTimes("abc", 3);
let resultString = str.repeat(count);
repeat() 构造并返回一个新字符串,该字符串包含被链接在一块儿的指定数量的字符串的副本function
递归循环