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));

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM