携程前端笔试题第2题巨坑
发布于 2018-03-29 20:58 2190 次浏览 0 赞 最后一次编辑是 2018-03-29 20:57 来自 试题交流  

携程笔试第二题输入到底是一行还是很多行啊?我不知道为什么我的本地调试是对的,但是交上去什么都错

气死我了,做了45分钟这题

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

考完后和大家讨论,真的气死我了,只能说贵公司出题测试样例真的牛


好气啊啊啊啊啊啊


2 条回复
第二题我也是 71%
2018-03-29 21:11

第二题的详细解答(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 10:05
添加回复
回到顶部