Skip to content

Instantly share code, notes, and snippets.

@honux77
Last active August 29, 2015 14:24
Show Gist options
  • Select an option

  • Save honux77/1b3acdfd64d300aa1b51 to your computer and use it in GitHub Desktop.

Select an option

Save honux77/1b3acdfd64d300aa1b51 to your computer and use it in GitHub Desktop.
bad_ex5.js
//MY BAD ANSWER
//referenced by http://stackoverflow.com/questions/280634/endswith-in-javascript
var fs = require('fs');
fs.readdir(process.argv[2],function(err, data) {
if(err) throw err;
var dirs = data.toString();
var files = dirs.split(",");
for (var i = 0; i < files.length; i++) {
if (files[i].endsWith("." + process.argv[3]))
console.log(files[i]);
}
});
if (typeof String.prototype.endsWith != 'function') {
String.prototype.endsWith = function(str, suffix) {
return this.indexOf(suffix, this.length - suffix.length) !== -1;
}
}
//GOOD ANSER
/*
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)
})
})
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment