观察下面的规律,写一个函数accum

    1. accum("abcd"); // "A-Bb-Ccc-Dddd"
    2. accum("RqaEzty"); // "R-Qq-Aaa-Eeee-Zzzzz-Tttttt-Yyyyyyy"
    3. accum("cwAt"); // "C-Ww-Aaa-Tttt"

    思路:1,将字符串用split分割成字符串数组
    2,将字符串数组里的每个元素用map做处理
    3,将数组中的所有元素用join转换成一个字符串,并用分隔符分隔

    1. function accum (str) {
    2. if((typeof str!='string')){
    3. return false
    4. }
    5. else if(str === ''){
    6. return ''
    7. }
    8. else{
    9. arr = str.split('') // 把一个字符串分割成字符串数组 "how are you" -- how,are,you
    10. let newArr = []
    11. for(let i = 0;i < arr.length; i++){
    12. let p = /[a-zA-Z]/i
    13. let b = p.test(arr[i])
    14. if (b) {
    15. let newUp = arr[i].toUpperCase() // 把字符串转换为大写
    16. let newLow = arr[i].toLowerCase() // 把字符串转换为小写
    17. let newLows = ''
    18. for(let y = i; y > 0; y--){ // 将小写的字母重复y次
    19. newLows = newLows + newLow
    20. }
    21. newArr.push(newUp + newLows) //push()向数组末尾添加一个或多个元素并返回新的长度
    22. }
    23. }
    24. return newArr.join('-') // join()把数组中的所有元素转换为一个字符串,并用分隔符分隔
    25. }
    26. }
    1. function accum(chart){
    2. return chart.split('').map((value,index)=>value.toUpperCase()+(value.repeat(index)).toLowerCase()).join('-')
    3. }
    4. // map() 方法返回一个新数组,数组中的元素为原始数组元素调用函数处理后的值
    5. // 语法:array.map(function(currentValue,index,arr), thisValue)
    function accum(str) {
        let result = '';
    
        for (let i = 0; i < str.length; i++) {
            const char = str[i];
    
            result += char.toUpperCase() + char.toLowerCase().repeat(i) + '-';
        }
    
        return result.slice(0, -1);
    }
    
    function accum (str){
      var newStr = '';
      str.split('').map((item ,index)=> {
        newStr+=item.toUpperCase();
        for(let i=0;i<index;i++){
          newStr+=item.toLowerCase();
        }
        newStr+='-';
      });
      return  newStr.slice(0,newStr.length-1);
    }
    
    function accum(str) {
        var strJoin = "";
        for (var i = 0; i < str.length; i++) {
            var cur_first = str.substr(i, 1);
            var cur_str = cur_first.toUpperCase();
            for (var j = i; j > 0; j--) {
                cur_str += cur_first.toLocaleLowerCase();
            }
            if (i != str.length - 1) {
                cur_str += "-";
            }
            strJoin += cur_str;
        }
        return strJoin;
    }
    
    function accum(str){
        return Array.from(str).reduce((prev, next, index) => { 
            prev.push(next.toUpperCase() + next.toLowerCase().repeat(index)); 
            return prev;
        }, []).join('-');
    }
    
    const letterRepeat = (letter, num) => {
        if(num <= 0) {
            return ''
        }
        return letter.toLowerCase().repeat(num);
    };
    
    const letterRepeat1 = (letter, num) => {
        if(num <= 0) {
            return ''
        }
        const current = letter.toLowerCase();
        return current.padEnd(num, current);
    };
    
    const letterRepeat2 = (letter, num) => {
        if(num <= 0) {
            return ''
        }
        const current = letter.toLowerCase();
        return Array.from({ length: num }, () => current).join('')
    };
    
    const accum = (str) => {
        let result = '';
        for (let i = 0; i < str.length; i++) {
            const item = str[i];
            const cell = item.toUpperCase() + letterRepeat(item, i);
            if (i === 0) {
                result += cell;
            } else {
                result += `-${cell}`;
            }
        }
        return result;
    };
    
    const accum1 = str => {
        return str.split('').reduce((prev, next, index) => {
            if (index === 1) {
                prev = prev.toUpperCase();
            }
            return prev + '-' + next.toUpperCase() + letterRepeat(next, index);
        });
    };
    
    // lower
    const accum = (str) => {
        if ((typeof str === 'string') && str.constructor == String) {
            const arr = [];
            for (let index in str) {
                const start = str[index].toUpperCase();
                const lowerStart = start.toLowerCase();
                let chars = start;
                for (let i = 0; i < index; i++) {
                    chars += lowerStart;
                }
                arr.push(chars);
            }
            return arr.join('-');
        } else {
            console.log('传入的参数不是字符串');
        }
    }
    
    // better
    const accum = (str) => {
        if ((typeof str === 'string') && str.constructor == String) {
            const arr = [];
            for (let index in str) {
                const start = str[index].toUpperCase();
                const chars = start + str[index].toLocaleLowerCase().repeat(index);
                arr.push(chars);
            }
            return arr.join('-');
        } else {
            console.log('传入的参数不是字符串');
        }
    }
    
    function accum(s) {
        return s.split('').map((c, i) => (c.toUpperCase() + c.toLowerCase().repeat(i))).join('-');
    }
    
    function accum(str) {
        return Array.from(str).reduce((prev, next, curIndex) => {
                next = next.toLowerCase();
            return `${prev}-${next.toUpperCase()}${next.repeat(curIndex)}`
        })
    }
    
    function accum(str) {
      return str.split('').reduce((res, cur, index) => `${res}-${cur.toUpperCase()}${cur.toLowerCase().repeat(index)}`, '').slice(1);
    }
    
    /**
     * 字符串转换功能函数
     * @param {String} str 
     * @retrun {String} 转换后的字符串
     */
    const accum = (str = '') => {
      if (typeof str !== 'string') return new TypeError('请输入String类型');
      if (str === '' || str.length === 1) return str.toLocaleUpperCase()
      const result = [...str].reduce((preVal, curVal, index) => {
        preVal = index === 1 && preVal.toLocaleUpperCase() || preVal;
        return `${preVal}-${curVal.toLocaleUpperCase()}${curVal.toLocaleLowerCase().repeat(index)}`;
      });
      return result;
    };
    // 扩展运算符(spread)是三个点(...)。它好比 rest 参数的逆运算,将一个数组转为用逗号分隔的参数序列
    
    /**
     * @desc 字符串转换处理
     * @param str
     * @return {string}
     * @example abcd->A-Bb-Ccc-Dddd
     *
     */
    export const accum = (str = '') => {
        return str.split('').map((item, index) => item.toUpperCase() + item.toLowerCase().repeat(index)).join('-')
    }
    
    accum("abcd");    // "A-Bb-Ccc-Dddd"
    accum("RqaEzty"); // "R-Qq-Aaa-Eeee-Zzzzz-Tttttt-Yyyyyyy"
    accum("cwAt");    // "C-Ww-Aaa-Tttt"
    
    function accum(str) {
        return str.toLowerCase().split('').map((x, i) => {
            return x.toUpperCase() + x.repeat(i)
        }).join('-');
    }
    
    const accum = function (str) {
        return str.split("").map((item, index)=>{
            return item.toUpperCase() + Array.from({length:index}, () => item).join("")
        }).join("-");
     }
    
    function accum(str){
        let arr = str.split('');
        let strnew = '';
        arr.forEach((item,index) => {
            strnew+=item.toUpperCase()
            for(var i = 0;i<index;i++){
                strnew += item.toLowerCase();
            }
            if(index<arr.length-1){
                strnew+='-';
            }
        });
    return strnew;
    
    /**
     * 处理字符串
     * @description 将字符串输出按照他的字符*位数
     * @example abc -> A-Bb-Ccc
     * @param {string} str 
     */
    function accum(str) {
        // 检查类型
        if(typeof str !== 'string') return new TypeError('类型错误,请输入字符串');
        // 检查是否只有字母
        if(/[^a-z]/ig.test(str)) return new Error('字符串中只能含有字母')
    
        let strArr = str.toLowerCase().split('').map((one,index)=>{
            let transformStr = one.toUpperCase()+one.toLowerCase().repeat(index);
            return transformStr
        });
        return strArr.join('-')
    }
    
    function accum(str) {
        return Array.from(str).map((item, index) => {
            return item.toUpperCase().padEnd(index+1, item)
        }).join('-')
    }
    
    function accum (s) {
       let arr = []
       for (let i = 0; i < s.length; i++) {
        arr.push(new Array(2).join(s.charAt(i)).charAt(0).toUpperCase() + new Array(i+1).join(s.charAt(i)).toLocaleLowerCase())
       }
       return arr.join('-')
     }
    
    function accum(str){
      let count = 1;
      let result = "";
      for(let i = 0; i < str.length; i++) {
        result += str[i].toUpperCase();
        for(let j = 1; j < count; j++) {
          result += str[i].toLowerCase();
        }
        count++;
        if(i != (str.length - 1)) {
          result += '-';
        }
      }
    
      return result;
    }
    
    简化:   
    function accum(str){return str.split('').reduce((a,c,inx)=>{return a+c.toUpperCase()+c.toLowerCase().repeat(inx)+"-"},'').slice(0,-1)}
    
    function accum(str){
        var text=''
        for(var i=0;i<str.length;i++){
            text+=str[i].toUpperCase();    // += 如 x+=y  即x=x+y
            for(var j=0;j<i;j++){
                text+=str[i]
            }
            text+='-'
        }
        text=text.substring(0,text.length-1)    // substring()方法用于提取字符串中介于两个指定下标之间的字符
        return text;
    }
    
    function accum(str) {
        return str.replace(/\w{1}/gi, (match,index) =>{
            const strLeft = Array.from(new Array(index)).map(() => match.toLowerCase()).join('');
            return match.toUpperCase() + strLeft + '-';
        }).slice(0,-1);
    }
    
    function accum(str){
        str = str.split('');
        let arr = str.map((char, index) => {
            return char.toUpperCase() + char.toLowerCase().repeat(index)
        })
        console.log(arr.join('-'))
        return arr.join('-')
    }
    
    function accum(str) {
        //判断类型
        if(typeof str !== 'string') return;
    
        //已知第一个为大写切只有1个。写在循环外面。省去循环内的一个if判断。
        let ret = str.charAt(0).toUpperCase() + '-';
        for(var i = 1; i < str.length; i++) {
            ret += str.charAt(i).toUpperCase().padEnd(i + 1,  str.charAt(i).toLocaleLowerCase()) + '-';
        }
    
        //去掉最后的 “-”
        return ret.slice(0 ,ret.length - 1);
    }
    
    function test(str) {
        let string = ''
        for (let i = 0; i < str.length; i++) {
          let tmp = ''
          for (let j = 0; j < str.indexOf(str[i]) + 1; j++) {
            tmp += str[i]
          }
          string += tmp[0].toUpperCase() + tmp.substring(1, tmp.length) + '-'
        }
        return string.substring(0, string.length - 1)
      }
    
    function accum(val) {
            return val.split('').map((k, i) => k.toUpperCase() + k.toLowerCase().repeat(i)).join('-')
    }
    
    function accum(word) {
        const l = word.length;
        if (!l) return;
        let newWord = '';
        for (let i = 0; i < l; i++) {
            newWord += word[i].toUpperCase() + word[i].toLowerCase().repeat(i) + (i !== l -1 ? '-' : '');
        }
        return newWord;
    }
    
    function accum(str) {
        var res = str.split('').map(function(item, index) {
            var a = '';
            for (var i = 0; i < index + 1; i++) {
                a += item;
            }
            return a;
        }).map(function(item, index, arr1) {
            return arr1[index][0].toUpperCase() + arr1[index].slice(1);
        }).join('-');
        return res
    }
    
    function accum(str) {
      // map来映射为新的数组
      str = str.split('').map((item,i) => {
        // repeat 重复字符串
        return item.toUpperCase() + item.toLowerCase().repeat(i)
      })
      return str.join('-')
    }
    
    function accum(value) {
        // 不校验参数合法性
        return Array.from(value).reduce((str, item, i) => {
            str += `${i == 0 ? '': '-'}${item.toUpperCase().padEnd(i + 1, item)}`
            return str
        }, '')    
    }
    
    function accum (str) {
        let ary = str.split('');
        let temStr = '';
        for (let i = 0; i < ary.length; i++) {
            let cur = ary[i]
            let a = cur.toUpperCase();
            let b = (new Array((i + 1)).join(cur.toLowerCase()));
            let c = i < ary.length - 1 ? '-' : '';
            temStr += (a + b + c);
        }
        return temStr
    }