刷題——Codewars Js題目(持續更新)

發現一個很好的練習作題網站 Codewars 數組

都是本身作過的,先放本身的答案,再放本身以爲不錯的其餘回答。網站

1. 將首字母放到後面並加上ay

pigIt('This is my string')轉換成:pigIt('hisTay siay ymay tringsay')code

  • mine排序

    function pigIt(str){
        var _str = [];
        str.split(' ').forEach((_value) => _str.push(_value.slice(1)+_value.slice(0,1)+'ay'));
        return _str.join(' ');
    }
  • others字符串

    function pigIt(str){
        return str.replace(/(\w)(\w*)(\s|$)/g, "\$2\$1ay\$3")
    }

2. 數組變成字符串而且最後一個用&鏈接

list([{name: 'Bart'},{name: 'Lisa'},{name: 'Maggie'},{name: 'Homer'},{name: 'Marge'}])轉換成:'Bart, Lisa, Maggie, Homer & Marge'get

  • minestring

    function list(names){
        if(names.length == 0)return '';
        let allname = [];
        names.forEach(function(_value){
            allname.push(_value.name);
        });
        let list = allname.join(",");
        var _index = list.lastIndexOf(",");
        list = list.replace(/,/g,function(a,b){
            return b == _index ? ' & ' : ', '
        });
        return list;
    }
  • othersit

    function list(names) {
        var xs = names.map(p => p.name)
        var x = xs.pop()
        return xs.length ? xs.join(", ") + " & " + x : x || ""
    }

3. 將0移到數組後面且其餘值在原來位置不排序

["a",0,"b",null,"c",0,"d",1,false,1,0,3,[],1,9,{},9,0]轉換成:["a","b",null,"c","d",1,false,1,3,[],1,9,{},9,0,0,0,0]io

  • mineconsole

    var moveZeros = function (arr) {
        let my_arr = arr.concat();
        let count = 0;
        arr.forEach(function(_value,_index){
            if(_value === 0){
                my_arr.push(...my_arr.splice(_index-count,1));
                count++;
            }
        });
        return my_arr;
    }
  • others

    var moveZeros = function (arr) {
      return arr.filter(function(x) {return x !== 0}).concat(arr.filter(function(x) {return x === 0;}));
    }

4. 數組內奇數排序,偶數位置不變

[5, 3, 2, 8, 1, 4]轉換成:[1, 3, 2, 8, 5, 4]

  • mine

    function sortArray(arr) {
        var myarr = [],
            myindex = [];
        arr.map(function(a,b){
            if(a%2 !== 0){
                console.log(a)
                myarr.push(a);
                myindex.push(b);
            }
        });
        myarr.sort((a,b) => a - b);
        myindex.map(function(a,b){
            arr[a] = myarr[b];
        });
        return arr;
    }
  • others

    function sortArray(array) {
        const odd = array.filter((x) => x % 2).sort((a,b) => a - b);
        return array.map((x) => x % 2 ? odd.shift() : x);
    }

5. 字符串裏面的數字排序

"is2 Thi1s T4est 3a"轉換成:"Thi1s is2 3a T4est"

  • mine

    function order(words){
        var arr = words.split(' ');
        arr.sort(function(a,b){
            return /\d/.exec(a)[0] - /\d/.exec(b)[0];
        });
        return arr.join(' ');
    }
  • others

    function order(words){
        return words.split(' ').sort(function(a, b){
            return a.match(/\d/) - b.match(/\d/);
        }).join(' ');
    }

6. 拆分數字並相乘直至個位數

給出參數39拆分:3*9 = 27, 2*7 = 14, 1*4=4(返回執行次數:三次)

  • mine

    function persistence(num) {
        var count = 0;
        var Count = function(_num){
            var total = 1;
            if(_num.toString().length == 1){
                return count;
            }
            total = _num.toString().split('').map((a) => total = total * a).pop();
            count++;
            return Count(total);
        }
        return Count(num);
    }
  • others

    const persistence = num => {
        return `${num}`.length > 1 ? 1 + persistence(`${num}`.split('').reduce((a, b) => (a * b)))
        : 0;
    }

7. 匹配密碼

六位以上密碼,至少一個數字、一個大寫字母、一個小寫字母

  • mine

    function validate(password) {
        return /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])\w{6,}$/.test(password);
    }
  • others

    function validate(password) {
      return  /^[A-Za-z0-9]{6,}$/.test(password) &&
              /[A-Z]+/           .test(password) &&
              /[a-z]+/           .test(password) &&
              /[0-9]+/           .test(password) ;
    }
相關文章
相關標籤/搜索