Last active
August 29, 2015 14:24
-
-
Save honux77/1b3acdfd64d300aa1b51 to your computer and use it in GitHub Desktop.
bad_ex5.js
This file contains hidden or 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
| //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