Javascript中常見的邏輯題和解決方案_javascript技巧

來源:互聯網
上載者:User

一、統計數組 arr 中值等於 item 的元素出現的次數

function count(arr, item) {   var count = 0;   arr.forEach(function(e){     //e為arr中的每一個元素,與item相等則count+1     e == item ? count++ : 0;   });   return count; }function count(arr, item) {  return arr.filter(function(a){    return (a==item);  }).length}function count(arr, item) {var res;return (res = arr.toString().match(new RegExp("//b"+item+"//b","g")))?res.length:0;}function count(arr, item) {      var count = 0;      arr.map(function(a) {        if(a === item) {          count++;        }      });      return count;    }

二、為數組 arr 中的每個元素求二次方。不要直接修改數組 arr,結果返回新的數組

function square(arr) {  return arr.map(function(item,index,array){    return item*item;  })}function square(arr) {  //聲明一個新的數組存放結果   var a = [];   arr.forEach(function(e){     //將arr中的每一個元素求平方後,加入到a數組中     a.push(e*e);   });   return a; }function square(arr) {//複製一個arr數組  var newarr = arr.slice(0);  for (var i=0;i<newarr.length;i++){    newarr[i]= newarr[i]* newarr[i];  }  return newarr;}function square(arr) {  return arr.map(function(item){    return Math.pow(item, 2);  })}

三、將數組 arr 中的元素作為調用函數 fn 的參數

function argsAsArray(fn, arr) { return fn.apply(this, arr); }function argsAsArray(fn, arr) {  return function(para1,para2){     return para1.apply(this,para2);  }(fn,arr);}

四、完成函數 createModule,調用之後滿足如下要求:

     1、返回一個對象

     2、對象的 greeting 屬性值等於 str1,name 屬性值等於 str2

     3、對象存在一個 sayIt 方法,該方法返回的字串為 greeting屬性值 + ‘, ‘ +name屬性值

function createModule(str1, str2) {   var obj = {     greeting : str1,     name   : str2,     sayIt  : function(){       //兩個屬性前面都需要加上this       return this.greeting+", "+this.name;     }   };   return obj; }//使用建構函式法function createModule(str1, str2) {  function Obj(){    this.greeting = str1;    this.name = str2;    this.sayIt = function(){      return this.greeting + ', ' + this.name;    };  }  return new Obj();}//建構函式與原型組合function createModule(str1, str2) {  function CreateMod(){    this.greeting = str1;    this.name = str2;  }  CreateMod.prototype.sayIt = function(){    return this.greeting + ', ' + this.name;  }  return new CreateMod();}

五、已知 fn 為一個預定義函數,實現函數 curryIt,調用之後滿足如下條件:

      1、返回一個函數 a,a 的 length 屬性值為1(即顯式聲明 a 接收一個參數)

      2、調用 a 之後,返回一個函數 b, b 的 length 屬性值為 1

      3、調用 b之後,返回一個函數 c, c 的 length 屬性值為 1

      4、調用 c 之後,返回的結果與調用 fn 的傳回值一致

      5、fn的參數依次為函數 a, b, c 的調用參數

輸入例子:

var fn = function (a, b, c) {return a + b + c}; curryIt(fn)(1)(2)(3);function curryIt(fn) {   //擷取fn參數的數量   var n = fn.length;   //聲明一個數組args   var args = [];   //返回一個匿名函數   return function(arg){     //將curryIt後面括弧中的參數放入數組     args.push(arg);     //如果args中的參數個數小於fn函數的參數個數,     //則執行arguments.callee(其作用是引用當前正在執行的函數,這裡是返回的當前匿名函數)。     //否則,返回fn的調用結果     if(args.length < n){      return arguments.callee;     }else return fn.apply("",args);   } }function curryIt(fn) {  return function a(xa){    return function b(xb){      return function c(xc){        return fn.call(this,xa,xb,xc);      };    };  };}

六、數組中輸出元素位置

function indexof(arr,item){  for(var i = 0,len = arr.length;i<len;i++){    var ite = arr[i];    if(ite == item){      console.log(ite == item);      return i;    }else{      return -1;    }  }}function indexof(arr,item){  return arr.indexOf(item);}

七、數組求和

function sum(arr) {  return eval(arr.join("+"));};

八、刪除給定元素

  function remove(arr, item) {    for(var i=0, m=arr.length, res=[]; i<m; i++){      if(item === arr[i]) continue;      else res.push(arr[i]);    }    return res;  }function remove(arr, item) {  var newA=arr.slice(0);  for(var i=newA.indexOf(item);i>-1;i=newA.indexOf(item)){    newA.splice(i,1);  }  return newA;}

總結

Javascript中常見的邏輯題就總結到這了,不知道大家都學會了嗎?本文的內容對大家的學習或者工作還是有一定協助的,如果有疑問大家可以留言交流,謝謝大家對雲棲社區的支援。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.