This algorithm is relatively simple, we use a small example to demonstrate:
num
Concatenates a specified string of times in a loop num
, and returns an empty string if it is a negative number.
function repeat(str, num) { if(num<0){ return ""; }else if(num==1){ return str; }else{ s=""; for(i=1;i<=num;i++){ s + = str; } return s; } } repeat("abc", 3);