js:重复输出字符串中字符

复习了 重复输出一个字符串后,

重复输出一个字符串是

比如给定 str:abc  num:3

要求输出 abcabcabc

文章链接:https://www.cnblogs.com/mobu/p/9899062.html

 

之后,我研究起了 重复输出字符串中字符

比如给定 str:abc  num:3

要求输出 aaabbbccc

除了对字符串递归的方法,剩下的方法相当于把字符串分成数组,然后再用上一个方法输出

我的github:swarz,欢迎给老弟我++星星

/******************************************
abc --> aaabbccc
*******************************************/

var times = (str, num) => str.split('').map(e => e.repeat(num)).join('');
console.log('1', times('abc', 3));
console.log('一句代码:', ((str, num) => str.split('').map(e => e.repeat(num)).join(''))('abc', 3));

var times = (str, num) => str.split('').map(e => new Array(num + 1).join(e)).join('');
console.log('2', times('abc', 3));

var times = (str, num) => str.split('').map(e => Math.pow(10, num - 1).toString().replace(/1|0/g, e)).join('');
console.log('3', times('abc', 3));

// 遍历到递归+三元函数
var times = (str, num) => {
    var ss = ''
    var len = str.length

    function tt(len) {
        return --len >= 0 ? ss = tt(len) + str[len].repeat(num) : ''
    }
    return tt(len)
}
console.log('4', times('abc', 3));

  var times = (str, num, len = str.length) => --len >= 0 ? times(str, num, len) + str[len].repeat(num) : ''
console.log('5', times('abc', 3));

 

转载于:https://www.cnblogs.com/mobu/p/10468482.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值