幾道js題的解答


題目來自: http://www.cnblogs.com/TomXu/archive/2012/02/10/2342098.html

原創答案,肯能和出題者的標准答案不同,但至少能實現目標。

 

 

 

  • 找出數字數組中最大的元素(使用Math.max函數)
Math.max.apply(Math, arr);
  • 轉化一個數字數組為function數組(每個function都彈出相應的數字)
arr.map(function(xld){
return function(){return xld}
})
  •  給object數組進行排序(排序條件是每個元素對象的屬性個數)
function count(o){var i = 0; for(var c in o){i++}return i};
arr.sort(function(a, b){return count(a) - count(b)})
  • 利用JavaScript打印出Fibonacci數(不使用全局變量)
function f(xld){ return xld < 2 ? 1 : f(xld - 1) + f(xld-2)}
  •     實現如下語法的功能:var a = (5).plus(3).minus(6); //2
Number.prototype.plus = function(xld){return this + xld};
Number.prototype.minus = function(xld){return this - xld};
  •     實現如下語法的功能:var a = add(2)(3)(4); //9
    function add(x){
    add.valueOf = add.toString = function(){return x}
    function add(y){ x+=y; return add}
    return add;
    }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM