Skip to content

Instantly share code, notes, and snippets.

@Andsbf
Last active August 29, 2015 14:19
Show Gist options
  • Save Andsbf/096e249b5e2c6244a7b4 to your computer and use it in GitHub Desktop.
Save Andsbf/096e249b5e2c6244a7b4 to your computer and use it in GitHub Desktop.
Learnyounode - I
console.log("HELLO WORLD")
var toPrint = process.argv.slice(2).map(Number);
toPrint = toPrint.reduce(function(pv,cv){
return pv + cv;
});
console.log(toPrint);
var fs = require('fs');
var path = process.argv[2];
lFile=fs.readFileSync(path).toString();
lFile = lFile.split('\n');
console.log(lFile.length - 1);
var fs = require('fs');
var path = process.argv[2];
fs.readFile(path,function(error,data){
lFile=data.toString().split('\n');
console.log(lFile.length - 1);
});
var fs = require('fs');
function strEndsWith(str, suffix) {
suffix = '.' + suffix;
return str.match(suffix+"$")==suffix;
}
var path = process.argv[2];
var filterKind = process.argv[3];
fs.readdir(path,function(error,data){
data.forEach(function(line, index){
if (strEndsWith(line,filterKind)) console.log(line);
});
});
// \\ // \\ Better Solution // \\ // \\
// var fs = require('fs')
// var path = require('path')
// fs.readdir(process.argv[2], function (err, list) {
// list.forEach(function (file) {
// if (path.extname(file) === '.' + process.argv[3])
// console.log(file)
// })
// })
var extraModule = require('./06a_make_it_modular.js')
extraModule(process.argv[2],process.argv[3], function(err,data){
data.forEach(function(element){
console.log(element);
});
})
var fs = require('fs')
var path = require('path')
module.exports = function (dirName, extensionType, callback) {
var result = [];
fs.readdir(dirName, function (err, list) {
if (err) {return callback(err, null)};
list.forEach(function (file) {
if (path.extname(file) === '.' + extensionType) {
result.push(file)
}
})
callback(null, result)
});
}
http = require ('http')
var url = process.argv[2];
http.get(url, function(res) {
res.setEncoding('utf8');
res.on('data',function(d){
console.log(d);
})
}).on('error', function(e) {
console.log("Got error: " + e.message);
});
http = require ('http')
var url = process.argv[2];
var data_final = [];
http.get(url, function(res) {
res.setEncoding('utf8');
res.on('data',function(d){
data_final.push(d);
})
res.on('end',function(){
console.log(data_final.join('').length);
console.log(data_final.join(''));
})
}).on('error', function(e) {
console.log("Got error: " + e.message);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment