奔三路 - 你30岁前的学习路!

奔三路学习网

当前位置: 主页 > 前端面试通 > 笔经面经 >

2015阿里web前端笔试题(部分)

时间:2017-10-31 21:23来源:百面网 作者:
1.给定一个随机数组,数组可能包含数组(也就是说数组元素可能为数组)。要求用js实现一个函数,返回该数组中所有元素

1.给定一个随机数组,数组可能包含数组(也就是说数组元素可能为数组)。要求用js实现一个函数,返回该数组中所有元素。例如,例如:数组[2,3,[4,6,[1,8]],12,10],返回结果为:[2,3,4,6,1,8,12,10]

  function getArray(arr) {
    var arrs = [];
    for(var i=0,l=arr.length;i < l;i++) {
        if(!Array.isArray(arr[i])) arrs.push(arr[i]);
        else arrs = arrs.concat( getArray(arr[i]) );
    }
    return arrs;
}

2.用js实现随机选取10–100之间的不同的10个数字,存入一个数组,并降序排序

        var aArray = [];
        var i = 0;
        for (i = 0; i < 10; i++) {
          randomNub();
        }
        aArray.sort(function (a, b) {
           return b - a
        });
        document.write(aArray)
        function randomNub() {
          var nowNub = parseInt(Math.random() * 100);
          if (nowNub < 10) {
            randomNub();
            return;
          }
          for (var j = 0; j < aArray.length; j++) {
            if (nowNub == aArray[j]) {
              randomNub();
              return;
            }
          }
          aArray.push(nowNub);
    }
    ```


> 3.下面代码希望在点击每段内容的时候给它加个灰色背景,可是下面这段代码好像没有满足需求,请帮忙修改下。

for (var i = 0; i < document.getElementsByTagName('p').length; i++) {
  var item = document.getElementsByTagName('p') [i];
  (function (j) {
    document.getElementsByTagName('p') [i].onclick = function () {
      item.style.backgroundColor = '#eee';
    }
  }(i))
}
//修改后
for (var i = 0; i < document.getElementsByTagName('p').length; i++) {
  var item = document.getElementsByTagName('p') [i];
  (function (j) {
    document.getElementsByTagName('p') [i].onclick = function () {
      this.style.backgroundColor = '#eee';
    }
  }(i))
}
```

4.为字符串实现一个render方法,实现下面的变量替换功能

    var greeting = 'my name is ${name}, age ${age}';
    var result = greeting.render({name: 'XiaoMing', age: 11});
    console.log(result);  //my name is XiaoMing, age 11
    String.prototype.render = function (Object) {
      var _$this = this;
      var keys = new Array();
      for (var p in Object)
      if (Object.hasOwnProperty(p))
      keys.push(p);
      for (i = 0; i < keys.length; i++) {
        (function (index) {
          var s = '${' + keys[index] + '}';
          str = _$this.replace(s, Object[keys[index]]);
          _$this = str;
        }) (i);
      }
      return str
    }
    var greeting = 'my name is ${name}, age ${age}';
    var result = greeting.render({
      name: 'XiaoMing',
      age: 11
    });
    console.log(result);//my name is XiaoMing, age 11
   other:
    String.prototype.render = function (Object) {
      var _this = this;
      for (var p in Object)
      if (Object.hasOwnProperty(p))
      {
        var s = '${' + p + '}';
        str = _this.replace(s, Object[p]);
        _this = str;
      }
      return str
    }
    var greeting = 'my name is ${name}, age ${age}';
    var result = greeting.render({
      name: 'XiaoMing',
      age: 11
    });
    console.log(result); //my name is XiaoMing, age 11
    ```


> 5.对json格式中的某一字段进行排序

eg:var stuJson = [{ name: “daming”, age: 21, weight: 66, sex:”boy” }, { name: “lisa”, age: 19, weight: 45, sex:”girl” }, { name: “lili”, age: 20, weight: 50, sex:”boy”}]; //eg:按age升序 stuJson.sort(function(a,b){ return a.age – b.age; }); console.log(stuJson);//[ { name: ‘lisa’, age: 19, weight: 45, sex: ‘girl’ }, //{ name: ‘lili’, age: 20, weight: 50, sex: ‘boy’ }, //{ name: ‘daming’, age: 21, weight: 66, sex: ‘boy’ } ]


> 6.用一句语句对数组去最小或最大值

eg:var arr = [32,42,12,42,21,23,56,75,3,33,53,23,36]; var aMin = Math.min.apply(null,arr); console.log(min);//3 var aMax = Math.max.apply(null,arr);//75 console.log(aMax);

(责任编辑:admin)
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片
发布者资料
史密斯 查看详细资料 发送留言 加为好友 用户等级:注册会员 注册时间:2017-11-24 11:11 最后登录:2018-09-02 11:09

关注奔三路