~~ FILTERED LS ~~

Create a program that prints a list of files in a given directory,
filtered by the extension of the files. You will be provided a
directory name as the first argument to your program (e.g.
'/path/to/dir/') and a file extension to filter by as the second
argument.

For example, if you get 'txt' as the second argument then you will
need to filter the list to only files that end with .txt.

The list of files should be printed to the console, one file per line.
You must use asynchronous I/O.

----------------------------------------------------------------------
HINTS:

The `fs.readdir()` method takes a pathname as its first argument and a
callback as its second. The callback signature is:

function (err, list) { ... }

where `list` is an array of filename strings.

Documentation on the `fs` module can be found by pointing your browser
here:
C:\Users\dzhang\AppData\Roaming\npm\node_modules\learnyounode\node_api
tml

You may also find the standard JavaScript `RegExp` class helpful here.

我写的:

var fs = require('fs');
var patt1=new RegExp("\.dat");
var fliter = fs.readdir(process.argv[2],function(err,list){
for(var i = 0 ; i < list.length ; i++){ if(list[i].split(".").length > 1){
if(patt1.exec(list[i])){
console.log(list[i]);
}
}
}
})

网站的答案:

  var fs = require('fs')
var regex = new RegExp('\\.' + process.argv[3] + '$') fs.readdir(process.argv[2], function (err, list) {
list.forEach(function (file) {
if (regex.test(file))
console.log(file)
})
})

nodeschool.io 5的更多相关文章

  1. nodeschool.io 4

    ~~ MY FIRST ASYNC I/O! ~~ Write a program that uses a single asynchronous filesystem operationto rea ...

  2. nodeschool.io 3

    ~~ MY FIRST I/O! ~~ Write a program that uses a single synchronous filesystem operation toread a fil ...

  3. nodeschool.io 2

    ~~  BABY STEPS  ~~ Write a program that accepts one or more numbers as command-line arguments and pr ...

  4. nodeschool.io 10

    ~~ TIME SERVER ~~ Write a TCP time server! Your server should listen to TCP connections on port 8000 ...

  5. nodeschool.io 9

    ~~ JUGGLING ASYNC ~~ 其实就是一个循环,在循环里面输出的顺序,和排列后在外面的顺序不一样,这是为什么呢? 用第三方async包,直接报错了…… This problem is th ...

  6. nodeschool.io 8

    ~~ HTTP COLLECT ~~ Write a program that performs an HTTP GET request to a URL provided toyou as the ...

  7. nodeschool.io 7

    ~~ HTTP CLIENT ~~ Write a program that performs an HTTP GET request to a URL provided toyou as the f ...

  8. nodeschool.io 6

    ~~ MAKE IT MODULAR ~~ This problem is the same as the previous but introduces the concept ofmodules. ...

  9. NODESCHOOL

    来源:https://nodeschool.io/zh-cn/ 核心基础课程(Core) javascripting 学习 JavaScript 语言的基础,无需任何编程经验 npm install ...

随机推荐

  1. SqlSever基础 dateadd month 增加五个月

    镇场诗:---大梦谁觉,水月中建博客.百千磨难,才知世事无常.---今持佛语,技术无量愿学.愿尽所学,铸一良心博客.------------------------------------------ ...

  2. TCP连接的状态详解以及故障排查

    我们通过了解 TCP各个状态 ,可以排除和定位网络或系统故障时大有帮助. 一.TCP状态 LISTENING :侦听来自远方的TCP端口的连接请求 . 首先服务端需要打开一个 socket 进行监听, ...

  3. 高通平台msm8909 LK 实现LCD 兼容

    前段时间小米出现红米note2 换屏门,现在我们公司也要上演了:有两个供应商提供不同IC 的LCD panel. 软件区分的办法是读取LCD IC 的ID 寄存器,下面解析高通平台LK中LCD兼容的过 ...

  4. application/x-www-form-urlencoded

    在Form元素的语法中,EncType表明提交数据的格式 用 Enctype 属性指定将数据回发到服务器时浏览器使用的编码类型. 下边是说明: application/x-www-form-urlen ...

  5. BZOJ 1488: [HNOI2009]图的同构 polay

    题意:两个图AB同构:把A的顶点重新编号后与B一模一样.求n个顶点的图一共有多少个?(同构的算一种) 思路:边有n*(n-1)/2,这些边可以有可以没有,所以等同于边的颜色有两种.然后将n划分成循环节 ...

  6. CodeForces 478C Table Decorations

    Table Decorations Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u ...

  7. C#在泛型类中,通过表达式树构造lambda表达式

    场景 最近对爬虫的数据库架构做调整,需要将数据迁移到MongoDB上去,需要重新实现一个针对MongoDB的Dao泛型类,好吧,动手开工,当实现删除操作的时候问题来了. 我们的删除操作定义如下:voi ...

  8. FZU 2221 RunningMan(跑男)

    Problem Description 题目描述 ZB loves watching RunningMan! There's a game in RunningMan called 100 vs 10 ...

  9. 打开Domion 提示: 管理员ID过期

    今天打开Domion 提示 管理员ID过期,什么操作都做不了,如是在网上趴了下,发现以下方法好用: 管理员ID文件被设置为允许超期,同时又没有其他ID文件可以用于访问服务器.如果尝试用已经超期的管理员 ...

  10. 无法在web服务器上启动调试。打开的URL的IIS辅助进程当前没有运行。

    调试时弹出:无法在web服务器上启动调试.打开的URL的IIS辅助进程当前没有运行.