CVTE前端二面

CVTE进度很快,一面当天晚上就通知要二面了,结果等着等着说改成第二天上午了,二面是视频面,面试官人看起来还可以,但是出了两个JS算法题,我写不上就不要让我写了啊(大哭),一直让写。。。

这次面试长了教训吧,要多看看类似的JS算法题,嗯,正如所料的没通过。。

第一题写了好长时间,有点懵逼了,面试官还一直让在那写,哎,第二题真的很简单啊,然而心态炸裂;

事实证明,该复习一下这种算法题了

第一题


var data = [
    { 
        parentId: 0, 
        id: 1, 
        value: '1' 
    }, 
    { 
        parentId: 3, 
        id: 2, 
        value: '2' 
    },
    { 
        parentId: 0, 
        id: 3, 
        value: '3' 
    },
    { 
            : 1, 
        id: 4, 
        value: '4' 
    }, 
    { 
        parentId: 1, 
        id: 5, 
        value: '5' 
    }, 
];

/**
请把该数据整理为树状结构, 该树每个节点的结构如下, 
node = {
    children: [],
    id: id,
    value: value
}
**/

拖了这么多天,终于发出来了,,这种题真的是需要多练啊

function test(){

    var data = [
        { parentId: 0, id: 1, value: '1' },
        { parentId: 3, id: 2, value: '2' },
        { parentId: 0, id: 3, value: '3' },
        { parentId: 1, id: 4, value: '4' },
        { parentId: 1, id: 5, value: '5' }
    ];

    var result = [];
    for(var i = 0;i<data.length;i++){
        var temp = [];   //存放data[i]的children数组
        data.forEach(function(item){
            if(item.parentId == data[i].id){
                temp.push(item);
            }
        });

        var node = {};
        node.children = [];
        temp.forEach(function(item){
            node.children.push(item);
        });
        node.id = data[i].id;
        node.value = data[i].value;

        result.push(node);

    }
    console.log(result);

}
test();

第二题

/**
请手动实现一个compose函数,满足以下功能:

例:

var arr = [func1, func2, func3];
function func1 (ctx, next) {
  ctx.index++
  next();
}
function func2 (ctx, next) {
  setTimeout(function() {
    ctx.index++
    next();
  });
}
function func3 (ctx, next) {
  console.log(ctx.index);
}

compose(arr)({index: 0}); 

// out: 2

**/

//答案
function compose(arr){
   let index = 0;
   return function(ctx){
        function next(){
          index++;
         arr[index](ctx, next)
        }
        arr[index](ctx, next)
   }
}

猜你喜欢

转载自blog.csdn.net/weixin_37719279/article/details/81281877
今日推荐