This file contains 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
// this is an idiomatic example of a Node async call with callbacks that | |
// handle both returned data and error | |
/***********This is the module defined for later use */ | |
var fs = require('fs'); | |
module.exports = function(file, ext, callback) { | |
fs.readdir(file, function(err, data) { | |
// if readdir returns error, call callback with error early |
This file contains 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 http = require('http'); | |
var bl = require('bl'); | |
// file path is from enviroment variable passed in through CLI | |
var urls = process.argv.slice(2); | |
var results = []; | |
var returned = 0; | |
function httpGet(index) { |
This file contains 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 http = require('http'); | |
var fs = require('fs'); | |
var url = require('url'); | |
var streamMap = require('through2-map'); | |
var port = process.argv[2]; | |
var path = process.argv[3]; | |
var server = http.createServer(function(request, response) { | |
var get = url.parse(request.url, true); |
This file contains 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 http = require('http'); | |
var fs = require('fs'); | |
var url = require('url'); | |
var streamMap = require('through2-map'); | |
var port = process.argv[2]; | |
var path = process.argv[3]; | |
var server = http.createServer(function(request, response) { | |
var get = url.parse(request.url, true); |
This file contains 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 names = ['jon', 'jane', 'jack']; | |
// something we want to do with each item in array | |
var log = function(a) { | |
console.log(a); | |
} | |
// using for loop | |
for (var i = 0; i < names.length; i++) { | |
log(names[i]); |
This file contains 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
/* how do you encapsulate a for loop within a function | |
so that each(names, callback) will do the same thing as the for loop? */ | |
// ensure that the all inputs are explicity declared in function signature | |
var each = function(array, callback) { | |
for (var i = 0; i < array.length; i++) { | |
callback(array[i]); | |
} | |
}; |
This file contains 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
// this is better | |
function plusOne(number) { | |
return number++; | |
} | |
plusOne(5); // 6 | |
// this is worse | |
var number = 5; | |
function plusOne() { |
This file contains 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 i; | |
var names = ['jon', 'jane', 'jack']; | |
// imperative programming | |
for (i = 0; i < names.length; i++) { | |
names[i] = names[i][0].toUpperCase() + names[i].slice(1); | |
} | |
console.log(names) // ['Jon', 'Jane', 'Jack'] | |
console.log(i) // 3 |
This file contains 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 i; | |
var names = ['jon', 'jane', 'jack']; | |
// imperative programming | |
for (i = 0; i < names.length; i++) { | |
names[i] = names[i][0].toUpperCase() + names[i].slice(1); | |
} | |
console.log(names) // ['Jon', 'Jane', 'Jack'] | |
console.log(i) // 3 |
This file contains 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
names2 = ['kendra', 'kim', 'kampbell']; | |
// use this as your callback function in map | |
var capitalize = function(word) { | |
return word[0].toUpperCase() + word.slice(1); | |
}; | |
// we need each implemented for map to work | |
var each = function(array, callback) { | |
for (var i = 0; i < array.length; i++) { |