This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function repeat(operation, num) { | |
| if (num <= 0) { | |
| return num | |
| } | |
| else { | |
| operation() | |
| num-- | |
| return num | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function repeat(operation, num) { | |
| if (num <= 0) return | |
| operation() | |
| // release control every 10 or so | |
| // iterations. | |
| // 10 is arbitrary. | |
| if (num % 10 === 0) { | |
| setTimeout(function() { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function Spy(target, method) { | |
| var originalFunction = target[method] | |
| var result = { | |
| count: 0 | |
| } | |
| target[method] = function() { | |
| result.count++ | |
| return originalFunction.apply(target, arguments) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module.exports = function arrayMap(arr, fn) { | |
| return arr.reduce(function (acc, current) { | |
| acc.push(fn(current)) | |
| return acc | |
| }, []) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module.exports = function(namespace) { | |
| return console.log.bind(console, namespace) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function duckCount() { | |
| return Array.prototype.slice.call(arguments).filter(function(obj) { | |
| return Object.prototype.hasOwnProperty.call(obj, 'quack') | |
| }).length | |
| } | |
| module.exports = duckCount |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module.exports = function reduce(arr, fn, initial) { | |
| function justDoOne(index, value) { | |
| if (index >= arr.length) { | |
| return value | |
| } | |
| return justDoOne(index + 1, fn(value, arr[index])) | |
| } | |
| return justDoOne(0, initial) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module.exports = function countWords(arr) { | |
| return arr.reduce(function(prev, curr) { | |
| prev[curr] = prev[curr] + 1 || 1 | |
| return prev | |
| }, {}) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var express = require('express') | |
| var fs = require('fs') | |
| var app = express() | |
| app.get('/books', function (req, res) { | |
| fs.readFile(process.argv[3], function (err, data) { | |
| var jsonFromFile = JSON.parse(data.toString()); | |
| res.json(jsonFromFile) | |
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var express = require('express') | |
| var app = express() | |
| app.get('/search', function (req, res) { | |
| res.send(req.query) | |
| }) | |
| app.listen(process.argv[2]) |