首页 > 代码库 > js题集24

js题集24

1.实现MagicFunction

MagicFunction(3) == 3; // should return true

MagicFunction(1, 2) == 3; // should return true

MagicFunction(1, 3)(2) == 6; // should return true

MagicFunction(1, 2)(3, 4, 5)(6)(7, 10) == 38; // should return true *1+2+3+4+5+6+7+10=38;

 

2.实现如下结果

seven(times(five())); // must return 35

four(plus(nine())); // must return 13

eight(minus(three())); // must return 5

six(dividedBy(two())); // must return 3

 

//your code........补充下面代码

 

function zero() {}

function one() {}

function two() {}

function three() {}

function four() {}

function five() {}

function six() {}

function seven() {}

function eight() {}

function nine() {}

 

function plus() {}

function minus() {}

function times() {}

function dividedBy() {}

 

3.实现lazy函数

new Lazy()

      .add(filterNumbers)

      .add(filterRange, 2, 7)

      .add(max)

      .invoke([1, 8, 6, [], "7", -1, {v: 5}, 4]); //6

 

//你可能需要下列函数用来测试结果

function max() {

    return Math.max.apply(null, arguments);

}

 

function filterNumbers() {

  return Array.prototype.filter.call(arguments, function(value) {

    return isNumeric(value);

  });

}

 

function isNumeric(n) {

  return !isNaN(n) && Number(n) === n;

}

 

function filterRange(min, max) {

  var args = Array.prototype.slice.call(arguments, 2);

  return Array.prototype.filter.call(args, function(value) {

    return min <= value && value <= max;

  });

}

 

4.实现--自己猜

function func1(arg1, arg2) { ... }

 

var map = createArgumentMap(func1,‘valueOfArg1‘, ‘valueOfArg2‘);

console.log(map[‘arg1‘]);  // writes ‘valueOfArg1‘

console.log(map[‘arg2‘]);  // writes ‘valueOfArg2‘

 

5.实现extend 

extend( {a: 1, b: 2}, {c: 3} ) // should === {a: 1, b: 2, c: 3}

extend( {a: 1, b: 2}, {c: 3}, {d: 4} ) // should === {a: 1, b: 2, c: 3, d: 4}

extend( {a: 1, b: 2}, {a: 3, c: 3} ) // should  === {a: 1, b: 2, c: 3}

extend( {a: false, b: null}, {a: true, b: 2, c: 3} ) // should  === {a: false, b: null, c: 3}

。。。。。。。。

//补充:原出题者情况说明较少 笔者补充一个arguments的情况过滤掉非object

//NODE 下 可以直接用isObject函数进行 非node下

//参考如下:

//

function isObject(val) {

    if (val === null) { return false;}

    return ( (typeof val === ‘function‘) || (typeof val === ‘object‘) );

}

 

1)arguments=

{

‘0‘: { a: 1, b: 2, length: 6 },

‘1‘: [],

‘2‘: ‘nope‘,

‘3‘: false,

‘4‘: [Function],

‘5‘: { c: 3, a: 3 }

}//   要过滤掉1,2,3,4

js题集24