领英前端实习背靠背面

第一轮:

第一题

参考

第二题:
输入数组endorsements:
输入格式是
endorsements = [{skill: ‘javascript’, user: ‘user1’}, {skill: ‘css’, user: ‘user2’}, {skill: ‘html’, user: ‘user3’}, {skill: ‘javascript’, user: ‘user2’}, {skill: ‘css’, user: ‘user3’}, {skill: ‘javascript’, user: ‘user3’}]
输出
[{skill: ‘javascript’, users:[‘user1’, ‘user2’, ‘user3’], count: 3}, {skill: ‘css’, users:[‘user2’, ‘user3’], count: 2}, {skill: ‘html’, users: [‘user3’], count: 1}]
follow up: 输出需要按照count排序

用了一个map把skill作为key,遍历endorsements数组处理,然后用Object.values(map)输出结果。

var sort = function(endorsements){
 var answer = [];
 if (endorsements === undefined || endorsements.length <= 2){
   return answer;
 }
 
 var map = {};
 for (var i = 0; i < endorsements.length; i++){
   if (map[endorsements[i].skill] === undefined){
     map[endorsements[i].skill] = [endorsements[i].user]; 
   }
   else {
     map[endorsements[i].skill].push(endorsements[i].user);
   }
 }
 
 for (var key in map){
   let object = {'skill': key, 'user': map[key], 'count': map[key].length};
   answer.push(object);
 }
 answer.sort(function (a, b) {
   if (a.count > b.count) {
       return -1;
   }
   if (b.count> a.count) {
       return 1;
   }
   return 0;
});
 return answer;  
 }

第三题考了

写个function isPalindrome
给了两个例子
console.log(isPalindrome(“racecar”)) // true
console.log(isPalindrome(“dog”)) // false

var isPalindrome = function(string){
// racecar
//    l    
//    r
if (string == undefined || string.length <= 2){
 return true;
}
let left = 0, right = string.length - 1;
 
while (left < right){
  if (string.charAt(left) != string.charAt(right)){
      return false;
  }
  left++;
  right--;
}
 
return true;
}

followup: 需要skip space

第二轮:

  • favorite website and why
  • event bubbling
  • class inheritance
  • css preprocessor

题是将link插入到包含每一个用户信息的div “content”里。link的代码是:

<ul>
<li>
<a href="profile.jsp?id=<memeber.id>"><member.name></a>
</li>
...
</ul>

考点是DOM的操作,怎么向DOM Tree里添加新的节点。 follow up是提高性能
然后考了 event prevent propagation