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

posted @ 2023-07-07 18:50  万物有序  阅读(12)  评论(0编辑  收藏  举报