Created
November 9, 2015 23:12
-
-
Save seemonz/23e5ee95e36ae6707aa0 to your computer and use it in GitHub Desktop.
learnyounode
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
// ex1 | |
console.log('HELLO WORLD'); | |
// ex2 | |
var total = 0; | |
for (i=2; i < process.argv.length; i++) { | |
total += Number(process.argv[i]); | |
} | |
console.log(total); | |
// ex3 | |
var fs = require('fs'); | |
var file = fs.readFileSync(process.argv[2]); | |
file = file.toString(); | |
var arr = file.split('\n'); | |
console.log(arr.length - 1); | |
// ex4 | |
var fs = require('fs'); | |
fs.readFile((process.argv[2]), 'utf8', findLines); | |
function findLines(err, data) { | |
var arr = data.split('\n'); | |
console.log(arr.length - 1); | |
} | |
// ex5 | |
var fs = require('fs'); | |
var path = require('path'); | |
fs.readdir(process.argv[2], list); | |
function list(err, data) { | |
var arr = data.filter( function(value) { | |
return (('.' + process.argv[3]) === path.extname(value)); | |
}); | |
arr.forEach( function(l){ | |
console.log(l); | |
}); | |
} | |
// ex6 | |
var read = require('./extension-reader.js') | |
// console.log(read); | |
read(process.argv[2], process.argv[3], logg); | |
function logg(err, data) { | |
if (err) { | |
console.log('there was an error,' + err); | |
} else { | |
data.forEach( function(l) { | |
console.log(l); | |
}); | |
} | |
} | |
// module | |
var fs = require('fs'); | |
var path = require('path'); | |
module.exports = function(dir, fileType, callback){ | |
fs.readdir(dir, function(err, data) { | |
if (err) { | |
return callback(err); | |
} | |
var arr = data.filter( function(value) { | |
return (('.' + fileType) === path.extname(value)); | |
}); | |
callback(null, arr); | |
}); | |
} | |
// ex7 | |
var http = require('http'); | |
http.get(process.argv[2], logg); | |
function logg(resp) { | |
resp.on('data', logData).setEncoding('utf8'); | |
} | |
function logData(l){ | |
console.log(l); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment