Skip to content

Instantly share code, notes, and snippets.

@bacalj
Last active August 29, 2015 14:00
Show Gist options
  • Save bacalj/11344932 to your computer and use it in GitHub Desktop.
Save bacalj/11344932 to your computer and use it in GitHub Desktop.
Exercise 5 of NodeSchool.io Learnyounode
//The code below passes the test - just writing down the process and a lingering question...
//First I had it testing for a hard-coded '.md' - that passed, but
//It only passed because the test happened to be wrriten for .md (?)
//then I put the second command line argument in as the test process.argv[3]
//but that didn't work
//saw the solution after passing the test and it had the addition of a '.'
//did the same to mine, and it now passes
//inspected process.argv[3] and it was indeed '.txt'
//Sooooo - why the need for the '.'? Regular expression trick?
var fs = require('fs');
var path = require('path')
fs.readdir(process.argv[2], function callback (err, list){
resultsArr = [];
for (var i = 0; i < list.length; i++) {
if (path.extname(list[i]) == '.' + process.argv[3]) {
resultsArr.push(list[i]);
}
};
var result = resultsArr.join("\n")
console.log(result);
/* inspector: console.log(process.argv[3]) */
});
@a-pasquale
Copy link

The function path.extname(p) returns a string containing the last . and everything after it, so it won't match unless you add the '.' to the argument. http://nodejs.org/api/path.html#path_path_extname_p

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment