• js的6道基础题(笔试常考题)


    转载:http://www.bubuko.com/infodetail-20477.html

    题目一:找出数字数组中最大的元素

    1.  
      var arr=[0,1,2,3,4,5,6,7,8,9];
    2.  
      console.log(Math.max.apply(null,arr))

    题目二:转化一个数字数组为function数组(每个function都弹出相应的数字)

    for循环闭包的问题

    1.  
      var arr=[0,1,2,3,4,5,6,7,8,9],arrFunc = [];
    2.  
      for(var i = 0, l = arr.length; i < l; i++){
    3.  
      arrFunc.push((function(i) {
    4.  
      return function() {
    5.  
      console.log(arr[i]);
    6.  
      }
    7.  
      })(i))
    8.  
      }
    题目三:给object数组进行排序(排序条件是每个元素对象的属性个数)
    1.  
      Object.prototype.myLength = function(){
    2.  
      var length = 0;
    3.  
      for(var i in this){
    4.  
      length ++;
    5.  
      }
    6.  
      return length;
    7.  
      }
    8.  
      var objArr = [
    9.  
      {a:1, b:2, c:5, d:7, e:8, g:0, h:12, i:5, v:9, w:9, x:9, y:9, z: 15},
    10.  
      {a:2, b:2, c:5, d:7, e:8, g:0, h:12, i:5, j:7, k:5, l:9, m:9, n:0, o:1, p:9, x:9, y:9, z:9 },
    11.  
      {a:3, b:2, c:5, d:7, e:8, g:0, h:12, i:5, j:7, k:5, l:9, m:9, n:0, o:1, p:9, q:0 },
    12.  
      {a:4, b:2, c:5, d:7, e:8, g:0, h:12, i:5, j:7, k:5, w:9, x:9, y:9, z:9 },
    13.  
      {a:5, b:2, c:5, d:7, e:8, g:0, h:12, i:5, j:7, k:5, v:9, w:9, x:9, y:9, z:9 },
    14.  
      {a:6, b:2, c:5, d:7, e:8, g:0, h:12, i:5, j:7, k:5, l:9, m:9, n:0, o:1, p:9, q:0, r:8, s:9, t:9, z:9 },
    15.  
      {a:7, b:2, c:5, d:7, e:8, x:9, y:9, z:9 }
    16.  
      ];
    17.  
      // arr before sort
    18.  
      var numArr1 = []
    19.  
      for(var i = 0, l = objArr.length; i < l; i++ ){
    20.  
      numArr1.push( objArr[i].myLength() )
    21.  
      }
    22.  
      console.log(numArr1.join(" ")) //result
    23.  
      // arr after sort
    24.  
      objArr.sort(function(a,b){
    25.  
      // stable sort
    26.  
      // return (a.myLength() > b.myLength()) === true? 1:-1;
    27.  
      // unstable sort
    28.  
      return (a.myLength() >= b.myLength()) === true? 1:-1;
    29.  
      // return a.myLength() - b.myLength();
    30.  
      })
    31.  
      var numArr2 = []
    32.  
      for(var i = 0, l = objArr.length; i < l; i++ ){
    33.  
      // console.log(i,l,objArr[i].myLength());
    34.  
      numArr2.push( objArr[i].myLength() )
    35.  
      }
    36.  
      console.log(numArr2.join(" ")) //result

    题目四:利用JavaScript打印出Fibonacci数(不使用全局变量)
    1.  
      var fibonacci = (function(){
    2.  
      var s = [];
    3.  
      var fun = function(x) {
    4.  
      if(s[x]){
    5.  
      return s[x];
    6.  
      }
    7.  
      if(x < 0) {
    8.  
      throw "Can‘t be negative";
    9.  
      return ;
    10.  
      }
    11.  
      else if(x === 0 || x === 1) {
    12.  
      s[x] = s[x] || x;
    13.  
      return s[x];
    14.  
      }
    15.  
      else{
    16.  
      s[x] = ( fun(x - 1) + fun(x - 2) );
    17.  
      return s[x];
    18.  
      }
    19.  
      };
    20.  
      fun.print = function() {
    21.  
      console.log(s.join(" "));
    22.  
      }
    23.  
      fun.printLast = function() {
    24.  
      // console.log(s.length);
    25.  
      return(s[s.length-1]);
    26.  
      }
    27.  
      window.s = s;
    28.  
      return fun;
    29.  
       
    30.  
      })()
    31.  
      console.time(200);
    32.  
      console.log(fibonacci(200));
    33.  
      console.log(fibonacci.printLast());
    34.  
      console.log(fibonacci.print());
    35.  
      console.timeEnd(200);
    1.  
      var fibonacci2 = function(x){
    2.  
      if(x < 0) {
    3.  
      throw "Can‘t be negative";
    4.  
      return ;
    5.  
      }
    6.  
      if(x === 0 || x === 1) {
    7.  
      return x;
    8.  
      }
    9.  
      var num = ( fibonacci2(x - 1) + fibonacci2(x - 2) )
    10.  
      return num;
    11.  
      }
    12.  
      console.time(32);
    13.  
      console.log(fibonacci2(32));
    14.  
      console.timeEnd(32);

    题目五:实现如下语法的功能:var a = (5).plus(3).minus(6);

    1.  
      Number.prototype.plus = function(x) {
    2.  
      var num = this.valueOf() + x;
    3.  
      return Number(num);
    4.  
      }
    5.  
      Number.prototype.minus = function(x) {
    6.  
      var num = this.valueOf() - x;
    7.  
      return Number(num);
    8.  
      }
    9.  
       
    10.  
      var a = (5).plus(3).minus(6);
    11.  
      console.log(a);
    12.  
      alert(a);
    题目六:实现如下语法的功能:var a = add(2)(3)(4);
    1.  
      function add(x) {
    2.  
      var mid;
    3.  
      mid = x || 0;
    4.  
      function addObj(x) {
    5.  
      x = x || 0;
    6.  
      mid = mid + x;
    7.  
      return addObj;
    8.  
      }
    9.  
      addObj.valueOf = function() {
    10.  
      return mid;
    11.  
      }
    12.  
      addObj.toString = function() {
    13.  
      return mid;
    14.  
      }
    15.  
      return addObj;
    16.  
      }
    17.  
      //call the obj.valueOf function
    18.  
      console.log(add(2));
    19.  
      console.log(add(2)(3));
    20.  
      console.log(add(2)(3)(4));
    21.  
      console.log(add(2)(3)(4)(5));
    22.  
       
    23.  
      //call the obj.toString function
    24.  
      alert(add(2));
    25.  
      alert(add(2)(3));
    26.  
      alert(add(2)(3)(4));
    27.  
      alert(add(2)(3)(4)(5));
  • 相关阅读:
    storm学习之七-storm UI页面参数详解
    kafka学习之-KafkaOffsetMonitor后台监控
    hbase深入了解
    storm学习之六-使用Maven 生成jar包多种方式
    kafka学习之-集群配置及安装
    Python的Web应用框架--Django
    plugins/python/uwsgi_python.h:2:20: fatal error: Python.h: No such file or directory
    key-value数据库-Redis
    SUSE 12安装详解
    分布式网络文件系统--MooseFS
  • 原文地址:https://www.cnblogs.com/lsongyang/p/10401151.html
Copyright © 2020-2023  润新知