2667. 创建 Hello World 函数
Question

Ans
|
/** * @return {Function} */ var createHelloWorld = function() { return function(...args) { return "Hello World"; } }; /** * const f = createHelloWorld(); * f(); // "Hello World" */ |
2620. 计数器
Question

Ans
|
/** * @param {number} n * @return {Function} counter */ var createCounter = function(n) { return function() { return n++; }; }; /** * const counter = createCounter(10) * counter() // 10 * counter() // 11 * counter() // 12 */ |
2704. 相等还是不相等
Question

Ans
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
/** * @param {string} val * @return {Object} */ var expect = function(val) { return {toBe:function(a){ if (val === a) { return true; } throw new Error("Not Equal"); },notToBe:function(a){ if (val !== a) { return true; } throw new Error("Equal"); }}; }; /** * expect(5).toBe(5); // true * expect(5).notToBe(5); // throws "Equal" */ |
2665. 计数器 II
Question

Ans
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
/** * @param {integer} init * @return { increment: Function, decrement: Function, reset: Function } */ var createCounter = function(init) { return { tmpV : init, increment:function(){ return ++init; },decrement:function() { return --init; },reset:function() { return (init = this.tmpV); } }; }; /** * const counter = createCounter(5) * counter.increment(); // 6 * counter.reset(); // 5 * counter.decrement(); // 4 */ |
2635. 转换数组中的每个元素
Question

Ans
|
/** * @param {number[]} arr * @param {Function} fn * @return {number[]} */ var map = function(arr, fn) { const len = arr.length; for (let i = 0; i < len; i++) { arr[i] = fn(arr[i], i); } return arr; }; |
2634. 过滤数组中的元素
Question

Ans
|
/** * @param {number[]} arr * @param {Function} fn * @return {number[]} */ var filter = function(arr, fn) { const filterArr = []; for (let i = 0; i < arr.length; i++) { if (fn(arr[i], i)) { filterArr.push(arr[i]); } } return filterArr; }; |
2626. 数组归约运算
Question

Ans
|
/** * @param {number[]} nums * @param {Function} fn * @param {number} init * @return {number} */ var reduce = function(nums, fn, init) { nums.forEach(item => { init = fn(init, item) }) return init }; |
2629. 复合函数
Question

Ans
|
/** * @param {Function[]} functions * @return {Function} */ var compose = function(functions) { return function(x) { return functions.reduceRight((target, fn) => fn(target), x) } }; /** * const fn = compose([x => x + 1, x => 2 * x]) * fn(4) // 9 */ |
2703. 返回传递的参数的长度
Question

Ans
|
/** * @return {number} */ var argumentsLength = function(...args) { return args.length; }; /** * argumentsLength(1, 2, 3); // 3 */ |
2666. 只允许一次函数调用
Question

Ans
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
/** * @param {Function} fn * @return {Function} */ var once = function(fn) { let isCalled = false; return function(...args){ if (isCalled) return undefined; isCalled = true; return fn(...args); } }; /** * let fn = (a,b,c) => (a + b + c) * let onceFn = once(fn) * * onceFn(1,2,3); // 6 * onceFn(2,3,6); // returns undefined without calling fn */ |
2623. 记忆函数
Question

Ans
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
/** * @param {Function} fn */ function memoize(fn) { const map = new Map(); return function(...args) { const item = args.join(','); if (!map.has(item)) { map.set(item, fn(...args)); } return map.get(item); } } /** * let callCount = 0; * const memoizedFn = memoize(function (a, b) { * callCount += 1; * return a + b; * }) * memoizedFn(2, 3) // 5 * memoizedFn(2, 3) // 5 * console.log(callCount) // 1 */ |
tmp
Question
Ans
本文为原创文章,版权归Aet所有,欢迎分享本文,转载请保留出处!
Everything will be better.