某公司的线上笔试题

 

1)编写正则表达式,匹配密码,要求密码必须以字母开头,必须有数字,字母,或   _  -  .  的组合 ,长度5-15位  

var reg = /^[a-zA-Z](?![a-zA-Z]+$)(?![0-9]+$)(?![-_.]+$)[a-zA-A\d-._]{4,16}$/g;

 

  ^   从头开始

  [a-zA-Z]        匹配的第一位是字母

  (?![a-zA-Z]+$)    向后预查到结尾 不匹配全是字母的情况

  (?![0-9]+$)       向后预查不匹配全是数字的情况

  (?![-_.]+$)      向后预查不匹配全是 - _  .的情况  

  [a-zA-Z\d.-_]{4,16}  因为前面第一位 的   []  消耗字符串, 所以剩余要匹配的长度减一  

  $          结尾      

这里主要运用到了   正则表达式的预查  

        更多关于  正则表达式 --->我的博客

  

2)用源生js 编写一个链式调用  new Box(100,100,red).addTo(body).addTo(body).delay(3000).remove();  实现 用过如上代码 向body 添加两个 100*100的红色的盒子,延时3S后移出

  小生不才  只是实现了效果,但是,应该有一部分不是这道题想要的

    function Box (wid,hei,col) {
      this.wid = wid || 0;
      this.hei = hei || 0;
      this.col = col || 'black';
      this.queue = [];    //队列储存生成的dom
    }
    Box.prototype.addTo = function(foo){    //这里也要注意,根据题,能在body插入多个Dom,所以,要生成的时候也要生成多个dom,
      this.foo = foo;                //所以   createElement要在这一步,  如果在上一步的话  new Box()只调用一次
      var box = document.createElement('div');  //这一步 插入多少次  只有一个dom(我线上笔试的时候就写错了)
      box.style.width = this.wid + 'px';
      box.style.height = this.hei + 'px';
      box.style.backgroundColor = this.col;
      document[foo].appendChild(box);
      this.queue.push(box);
      return this;
    }
    Box.prototype.delay = function(tim){ 
      return this;                  
    }                     
                          Box.prototype.remove
= function(){ var self = this; setTimeout(() => { self.queue.forEach(ele => { document[this.foo].removeChild(ele); }); }, this.tim); return this; }

 

 

//这个delay不会,去网上找的   , 
使用 循环占用cpu,但是他会在daaTo的dom显示出来之前就进入循环状态
,阻塞了前面dom生成的效果this.tim = tim;            
//循环占用cpu  还不能使用setTimeout ,
改变循环的判断值 ,
就像是 while(flag){} 开始flag是true,
计时器结束了 flag = false 
//然而这样是不行的,  改变了的 flag传不进去,  
如果把 计时器放在循环里头  那就是疯了。。。。
//网上的 做法是   
用 在循环里用  new Date().getTime() 和  
在循环外面 第一次执行就获取的 new Date().getTime()+timer 进行比较
//来当做定时器  (timer是指   定时器传进来的  毫秒数)

某公司的线上笔试题_第1张图片

 

3)已知对象 obj = {'asfsd':'3123,'dsf':'2131,'gads':'3213','sdaf':2526}  实现将对象的key 按照ascii的顺序排序(这道题应该考的是数组克隆)

    var obj = {'abc':123,'cde':345,'bac':213,'def':456}
    function clone(src,tar){       //最简单的一个克隆  不对原对象进行改变
      for(var prop in src) {
          tar[prop] = src[prop];    
      }
    }
    var tar = {};
    clone(obj,tar);    
    var arrKey = [];
    for(var i in tar){          //便利出key的数组
      arrKey.push(i)
    }
    var arrOrder = arrKey.sort()    
    console.log(arrOrder);

转载于:https://www.cnblogs.com/96weibin/p/8717102.html

你可能感兴趣的:(某公司的线上笔试题)