CVTE二面凉凉

真的要努力刷题。。。。。。
第一题:
将下面数据结构
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 node = {
   children:[
      {
          children:[
            {
                children:[],
                id: 2;
                value: '2'
            }
          ],
          id: 3;
          value: '3'
      },
      {
          children:[
            {
                children:[],
                id: 4;
                value: '4'
            },
            {
                children:[],
                id: 5;
                value: '5'
            }
          ],
          id: 1;
          value: '1'
      }
    ],
    id: 0;
    value: undefined
}

答案:
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 Node = function (id, value) {
    this.id = id;
    this.value = value;
    this.children = [];
}
var solve = {
    value: [],
    edge: [],
    init: function(arr) {
        arr.forEach(e => {
            //id-value键值对
            this.value[e.id] = e.value;
            //父子关系
            this.edge[e.parentId]?this.edge[e.parentId].push(e.id):this.edge[e.parentId]=[e.id];
        });
        return this.makeTree(this.getRoot());
    },
    getRoot: function(){
        var root = 0;
        while(this.value[root]!=undefined){
            root++;
        }
        return root;
    },
    makeTree: function(id){
        var node = new Node(id,this.value[id]);
        if(this.edge[id]){
            this.edge[id].forEach(e =>{
                node.children.push(this.makeTree(e));
            })
        }
        return node;
    }
}
solve.init(data);
console.log(solve.makeTree(0));

第二题:
/**
请手动实现一个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){
}  

答案:
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});
function compose(arr){
    return function(ctx){
        function next(){
            if(arr.length!=0){
                let func = arr.shift();
                func(ctx,next);
            }
        }
        if(arr.length!=0){
            let func = arr.shift();
            func(ctx,next)
        }
    }
}

#广州视源电子科技股份有限公司##面经##前端工程师##秋招##内推#
全部评论
加油!
点赞 回复
分享
发布于 2018-07-30 21:01
是前端岗位的远程面试吗?
点赞 回复
分享
发布于 2018-07-30 21:14
滴滴
校招火热招聘中
官网直投
第二题有点像koa-compose
点赞 回复
分享
发布于 2018-07-30 21:20
感觉现场面试也是要手撸算法了
点赞 回复
分享
发布于 2018-07-30 22:26
只有两道编程题吗,中间有问其他问题什么的吗
点赞 回复
分享
发布于 2018-07-30 23:23
加油~!
点赞 回复
分享
发布于 2018-07-31 15:29
请问一下  一面过了多久,出的结果?
点赞 回复
分享
发布于 2018-08-01 12:16
居然二面过了。。。。。。
点赞 回复
分享
发布于 2018-08-01 17:35
第一题我在拼多多笔试也见到了= = 不过不会写,有大佬能告知一下不
点赞 回复
分享
发布于 2018-08-01 17:51
和你题目一样,感觉是同一个面试官,同凉凉
点赞 回复
分享
发布于 2018-08-01 18:36

相关推荐

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