js 实现一个 repeat
1 function _repeat(str, num) { 2 if ( 3 typeof str !== "string" || 4 typeof num !== "number" || 5 num.toString().includes(".") || 6 num < 0 7 ) 8 throw Error("参数不合法"); 9 10 let s = ""; 11 while (num) { 12 if (num & 1) { 13 s += str; 14 } 15 num = num >> 1; 16 str += str; 17 } 18 return s; 19 }
THE END