~~ 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. .Net面試題

    初级.NET开发人员 - 任何使用.NET的人都应知道的 1. 描述线程与进程的区别? 进程是系统所有资源分配时候的一个基本单位,拥有一个完整的虚拟空间地址,并不依赖线程而独立存在.进程可以定义程序的 ...

  2. 【原文】前端程序员必须知道的高性能Javascript知识

    原文:前端程序员必须知道的高性能Javascript知识 想必大家都知道,JavaScrip是全栈开发语言,浏览器,手机,服务器端都可以看到JS的身影. 本文会分享一些高效的JavaScript的最佳 ...

  3. ubuntu12.04安装mysql

    首先下载ubuntu 12.04 64位对应的myqsl版本                 http://dev.mysql.com/downloads/file/?id=464508 然后按照如下 ...

  4. Java _ JDK _ Arrays, LinkedList, ArrayList, Vector 及Stack

    (最近在看JDK源码,只是拿着它的继承图在看,但很多东西不记录仍然印象不深,所以开始记录JDK阅读系列.) (一)Arrays Arrays比较特殊,直接继承自Arrays ->List(Int ...

  5. Perform Cut Copy Paste Operations Using Cut_Region Copy_Region Paste_Region Commands In Oracle Forms

    You can do Select, Cut, Copy and Paste operations on text items in Oracle Forms using Select_All, Cu ...

  6. session 登陆浏览,并实现session注销登陆

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...

  7. vsftpd配置参数详细整理

    vsftpd配置参数详细整理  -|白王斧三又干一 vsftpd配置参数详细整理     -|白王斧三又干一 发表于 2005-10-23 20:30:00   1.vsftpd配置参数详细整理#接受 ...

  8. [数据结构与算法]栈Stack的多种实现

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  9. EditPlus添加到右键菜单

    1.Alt+R 键打开“运行” 2.“运行”中输入:regedit 打开注册表    (1.在 "我的电脑HKEY_CLASSES_ROOT*" 下新建项 shell: (2.在 ...

  10. HTML笔记(三) 列表

    1. 无序ul标签: <html> <body> <h4>一个无序列表:</h4> <ul> <li>咖啡</li> ...