題目來自: 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;
}