习题1: 编写函数, 将输入字符串转成大写输出.
function upperCaser(input) { return input.toUpperCase();}module.exports = upperCaser;
习题2: 编写函数, 输入两个参数, 第一个参数为一函数, 第二个参数代表函数的执行次数.
function repeat(operation, num) { num = parseInt(num); while (num--) { operation(); }}module.exports = repeat;
习题3: 使用map函数对以下函数进行简化:
function doubleAll(numbers) { var result = [] for (var i = 0; i < numbers.length; i++) { result.push(numbers[i] * 2) } return result}module.exports = doubleAll
转换:
function doubleAll(numbers) { return numbers.map(function(item) { return item * 2; });}module.exports = doubleAll;
习题4: 输入一个数组, 数组的元素位{message: '***'}, 输出长度小于50的字符串组成的数组:
function getShortMessages(messages) { return messages.filter(function(item) { return item.message.length < 50; }).map(function(item) { return item.message; });}module.exports = getShortMessages;
习题5: 编写一个函数, 判断所输出的子数组是否在父数组中.
function checkUsersValid(goodUsers) { return function allUsersValid(submittedUsers) { return submittedUsers.every(function(item) { return goodUsers.indexOf(item) !== -1; }); };}module.exports = checkUsersValid;
习题6: 给予一个数组, 统计出现元素的个数:
function countWords(inputWords) { return inputWords.reduce(function(result, item) { result[item] = (result[item] || 0) + 1; return result; }, {});}module.exports = countWords;
习题7: 使用简易版的reduce函数
function reduce(arr, fn, initial) { if (!arr.length) return initial; return reduce(arr.slice(1), fn, fn(initial, arr[0]));}module.exports = reduce;
习题8: 编写函数, 判断所输入的参数中有quack属性的个数.
function duckCount() { var count = 0; for (var i = 0; i < arguments.length; i++) { if (Object.prototype.hasOwnProperty.call(arguments[i], 'quack')) { count++; } } return count;}module.exports = duckCount;
习题9: 实现简易版的log函数, 使用apply.
var info = logger('INFO:')info('this is an info message')// INFO: this is an info messagevar warn = logger('WARN:')warn('this is a warning message', 'with more info')// WARN: this is a warning message with more info
实现:
var slice = Array.prototype.slice;function logger(namespace) { return function log() { console.log.apply(null, [namespace].concat(slice.apply(arguments))); }}module.exports = logger;
习题10: 使用bind实现log函数
module.exports = function(namespace) { return console.log.bind(console, namespace);}
习题11: 使用reduce实现map
module.exports = function arrayMap(arr, fn) { return arr.reduce(function(result, item) { result.push(fn(item)); return result; }, []);};
习题12: 判断函数执行的次数, 例如:
var spy = Spy(console, 'error')console.error('calling console.error')console.error('calling console.error')console.error('calling console.error')console.log(spy.count) // 3
实现:
function Spy(target, method) { var m = target[method]; var result = { count: 0 }; target[method] = function() { result.count++; return m.apply(this, arguments); }; return result;}module.exports = Spy;
习题13: 非阻塞执行递归函数, 使用新的setImmediate函数.
function repeat(operation, num) { if (num <= 0) return; operation(); setImmediate(function() { repeat(operation, --num); });}module.exports = repeat;
习题14: repeat递归执行情况下, 会存在堆栈溢出(由于函数调用函数, 则调用过多就造成堆栈溢出).
解决方案就是: 函数执行完一次, 将第二次执行的函数赋值给第一个函数, 保证函数数量不会递增:
function repeat(operation, num) { return function() { if (num <= 0) return; operation(); return repeat(operation, --num); };}function trampoline(fn) { while (fn && typeof fn === 'function') { fn = fn(); }}module.exports = function(operation, num) { trampoline(function() { return repeat(operation, num); });};
备注: 此答案来自论坛给出的结果, 个人没想到如此解决.
习题15: 考虑如下函数:
function loadUsers(userIds, load, done) { var users = [] for (var i = 0; i < userIds.length; i++) { users.push(load(userIds[i])) } return users}module.exports = loadUsers
由于load为异步函数, 则代码有误! 修改如下:
function loadUsers(userIds, load, done) { if (!userIds.length) { done(); return; } load(userIds[0], function(result) { return loadUsers(userIds.slice(1), load, done); });}module.exports = loadUsers;
习题16: 递归一个dependencies, 获取其包+版本号, 并排序, 不允许重复值.
function getDependencies(tree) { var items = tree.dependencies; if (!items) return []; var arr = Object.keys(items).reduce(function(arr, k) { if (items[k].hasOwnProperty('version')) { arr.push(k + '@' + items[k].version); } if (items[k].hasOwnProperty('dependencies')) { arr = arr.concat(getDependencies(items[k])); } return arr; }, []); arr.sort(); arr = arr.filter(function(item, index) { return arr.indexOf(item) === index; }); return arr;}module.exports = getDependencies;
习题17和习题18不会做.....