console.log( ‘blah’.repeatMe( 3 ) );
使用 Javascript 编写代码,使前面的函数打印:
输出:blahblahblah
最佳答案
噢,放弃函数式解决方案太有趣了。
String.prototype.repeatMe = function(n) {
if (n <= 0) return "";
if (n%2 === 1) return (""+this) + this.repeatMe(n-1);
var half = this.repeatMe(n/2);
return half + half;
}
document.body.innerHTML = "tester".repeat(10)
作为练习,我会让你弄清楚发生了什么。
关于javascript 练习 - 带字符串的递归函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29134961/