携程2018秋招笔试题解(前端)

虽然携程的题很坑,但是题解还是要写的,总结反思吧
第一题:

参考代码:
let str="携程C2t0r1i8p2020校招";
let nums=str.match(/\d/g);
let chars=str.match(/[A-Za-z]/g);
nums=[...new Set(nums)];
let s="";
nums.forEach(e=>s+=e);
chars.forEach(e=>s+=e);
console.log(s);

第二题:


参考代码:(仅供参考,不一定能AC)
let str=[null, 2, "test", undefined, {
    "type": "product",
    "content": "product1"
},  {
    "type": "product",
    "content": "product2"
},  {
    "type": "tag",
    "content": "tag1"
}, {
    "type": "product",
    "content": "product3"
}, {
    "type": "tag",
    "content": "tag2"
}];
function groupList(list) {
    if (Object.prototype.toString.call(list) !== '[object Array]') 
        return [];     
    for(let i=list.length-1;i>=0;i--){
        if(list[i]==null||typeof list[i]!="object"){
            list.splice(i,1);
        }
    }
    let types=[];
    for(let i=list.length-1;i>=0;i--){
        types.push(list[i].type);
    }
    types=[...new Set(types)]
    let product=[],tag=[],res=[];
    for(let j=0;j<types.length;j++){
        let newType=[];
        for(let i=list.length-1;i>=0;i--){
            if(list[i].type==types[j]){
                newType.unshift(list[i].content);
            }
        }
        res.push(newType);
    }
    let res2=[];
    for(let j=types.length-1;j>=0;j--){
        let tmp={type:types[j],content:res[j]}
        res2.push(tmp);
    }
    return res2;
}
var data =str;
var result = groupList(data);
console.log(JSON.stringify(result));

第三题:
实现类似迭代方法中的map()方法
参考代码:
function map(data, fn) {
    let res;
    if (Object.prototype.toString.call(data) === '[object Array]') {
        res = []
        data.forEach(function (v, i, arr) {
            res.push(fn(v, i, arr));
        })
        return res;
    } else if (Object.prototype.toString.call(data) === '[object Object]') {
        res = {};
        for (let key in data) {
            res[key] = fn(data[key], key, data);
        }
        return res;
    }
    return null;
}

继续加油吧。。。。

#春招##携程#
全部评论
第一题数字去重,字符数字不用先转为数字类型再set么?
点赞 回复
分享
发布于 2018-03-29 22:54
第一个两个set然后join就好😂😂
点赞 回复
分享
发布于 2018-03-30 00:17
阅文集团
校招火热招聘中
官网直投
第二题的详细解答(JavaScript 版 AC): https://www.rayjune.me/2018/03/31/interesting-programming-problems-ctrip-2018-spring-recruit/#2-%E6%95%B0%E7%BB%84%E5%8D%87%E7%BB%B4
点赞 回复
分享
发布于 2018-03-31 09:49
第三题是啥来着。。
点赞 回复
分享
发布于 2018-04-07 01:07

相关推荐

点赞 22 评论
分享
牛客网
牛客企业服务