《JavaScript 框架设计》

版本1:

function repeat(target, n) {
return (new Array(n + 1)).join(target)
}

版本2:

function repeat(target, n) {
return Array.prototype.join.call({length: n + 1}, target)
}

版本3:(缓存)

var repeat = (function() {
var join = Array.prototype.join,
obj = {}
return function(target, n) {
obj.length = n + 1
return join.call(obj, target)
}
})();

版本4:(算法)

function repeat(target, n) {
var s = target,
total = []
while (n > 0) {
if (n % 2 == 1) total[total.length] = s
if (n == 1) break
s += s
n = n >> 1
} return total.join('')
}

版本5:

function repeat(target, n) {
var s = target,
c = s.length * n
do {
s += s
} while (n = n >> 1)
s = s.substring(0, c)
return s
}

版本6:

function repeat(target, n) {
var s = target
total = ''
while (n > 0) {
if (n % 2 == 1) total += s
if (n == 1) break
s += s
n = n >> 1
}
return total
}

版本7:(递归)

function repeat(target, n) {
if (n == 1) {
return target
}
var s = repeat(target, Math.floor(n / 2))
s += s
if (n % 2) {
s += target
}
return s
}

【Better Code】repeat的更多相关文章

  1. 【Leet Code】Palindrome Number

    Palindrome Number Total Accepted: 19369 Total Submissions: 66673My Submissions Determine whether an ...

  2. 【转】【VS Code】配置文件Launch及快捷键

     Ctrl+shift+p,然后输入launch,点击第一个选项即可配置. 之后选择More即可 具体配置可修改为: { "version": "0.2.0", ...

  3. 【Gray Code】cpp

    题目: The gray code is a binary numeral system where two successive values differ in only one bit. Giv ...

  4. 【繁星Code】如何在EF将实体注释写入数据库中

    最近在项目中需要把各个字段的释义写到数据库中,该项目已经上线很长时间了,数据库中的字段没有上千也有上百个,要是一个项目一个项目打开然后再去找对应字段查看什么意思,估计要到明年过年了.由于项目中使用En ...

  5. -_-#【Better Code】

    i++ 与 ++i 的性能区别 if (true) { console.log('hi') } if (!false) { console.log('hi~') } true && c ...

  6. -_-#【Better Code】throttle / debounce

    浅谈javascript的函数节流 javascript函数的throttle和debounce throttle 疯狂触发事件,固定步调执行 debounce 疯狂触发事件,不会执行 var res ...

  7. 【Better Code】面向切面编程

    用AOP改善JavaScript代码http://www.w3cfuns.com/thread-5597323-1-1.html

  8. -_-#【Better Code】字符串匹配

    提高 web 应用性能之 JavaScript 性能调优

  9. 【Leet Code】String to Integer (atoi) ——常考类型题

    String to Integer (atoi) Total Accepted: 15482 Total Submissions: 106043My Submissions Implement ato ...

随机推荐

  1. ZooKeeper(3.4.5) 使用Curator监听事件

    转载:http://www.mamicode.com/info-detail-494364.html 标签: ZooKeeper原生的API支持通过注册Watcher来进行事件监听,但是Watcher ...

  2. BZOJ3999 [TJOI2015]旅游

    题面:给定一个有$n$个节点的树,每个点又点权$v_i$,每次选取一条树链$[a, b]$,求出$max(v_i - v_j)$,其中$i, j \in [a, b]$且$i$出现在$j$前面,最后树 ...

  3. oracle建库及plsql建表空间的用法

    所有程序—>ORACLE-JHEMR----------->配置和移植工具----->DataBase Configuration Assistant-------中间就需要改一个数 ...

  4. curl,chkconfig

    1. Linux系统服务管理 工具ntsysv 类似图形界面管理工具,如果没有该命令使用 yum install -y ntsysv 安装 常用服务:crond, iptables, network, ...

  5. 使用WebView加载HTML代码

    使用EditText显示HTML字符串时,EditText不会对HTML标签进行任何解析,而是直接把所有HTML标签都显示出来-----就像用普通记事本显示一样:如果应用程序想重新对HTML字符串进行 ...

  6. if语句使用

    package yuan; public class Yuan { public static void main(String[] args) { int a = 1; int b = 8; int ...

  7. PHPExcel 学习笔记

    首先到phpexcel官网上下载最新的phpexcel类,下周解压缩一个classes文件夹,里面包含了PHPExcel.php和PHPExcel的文件夹,这个类文件和文件夹是我们需要的,把class ...

  8. centos 升级 python后 python-setuptools pip 安装依赖报错

    解决办法: $ wget https://svn.apache.org/repos/asf/oodt/tools/oodtsite.publisher/trunk/distribute_setup.p ...

  9. TDBGridEh的 搜索面板 TDBGridSearchPanel

    TCustomDBGridEh.Create FSearchPanelControl := TDBGridSearchPanelControlEh.Create(Self); //这里,创建的构造函数 ...

  10. CSU 1111 D(Contest #3)

              有三户人家共拥有一座花园,每户人家的太太均需帮忙整理花园.A 太太工作了5 天,B 太太则工作了4 天,才将花园整理完毕.C 太太因为正身怀六甲无法加入她们的行列,便出了90元.请 ...