zoukankan      html  css  js  c++  java
  • 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));
  • 相关阅读:
    Vue + Element 中的时间自定义选择框的数据传参绑定分析与js格式化时间参数
    vue 给data 数据的重新初始化
    Vue + Element 后台项目与后台的数据对接
    js将两组数据存到你定义的空的对象数组
    Element ui 自定义表格行样式
    vue组件的注册与使用
    Vue + Element 实现下拉选择统计时间数据栏并展示
    Vue + Element后台项目报错(This relative module was not found)
    Echart图的使用技巧,配置相关样式与属性
    7种方法实现数组去重
  • 原文地址:https://www.cnblogs.com/lsongyang/p/10401151.html
Copyright © 2011-2022 走看看