nodeschool.io 5
~~ 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的更多相关文章
- nodeschool.io 4
~~ MY FIRST ASYNC I/O! ~~ Write a program that uses a single asynchronous filesystem operationto rea ...
- nodeschool.io 3
~~ MY FIRST I/O! ~~ Write a program that uses a single synchronous filesystem operation toread a fil ...
- nodeschool.io 2
~~ BABY STEPS ~~ Write a program that accepts one or more numbers as command-line arguments and pr ...
- nodeschool.io 10
~~ TIME SERVER ~~ Write a TCP time server! Your server should listen to TCP connections on port 8000 ...
- nodeschool.io 9
~~ JUGGLING ASYNC ~~ 其实就是一个循环,在循环里面输出的顺序,和排列后在外面的顺序不一样,这是为什么呢? 用第三方async包,直接报错了…… This problem is th ...
- nodeschool.io 8
~~ HTTP COLLECT ~~ Write a program that performs an HTTP GET request to a URL provided toyou as the ...
- nodeschool.io 7
~~ HTTP CLIENT ~~ Write a program that performs an HTTP GET request to a URL provided toyou as the f ...
- nodeschool.io 6
~~ MAKE IT MODULAR ~~ This problem is the same as the previous but introduces the concept ofmodules. ...
- NODESCHOOL
来源:https://nodeschool.io/zh-cn/ 核心基础课程(Core) javascripting 学习 JavaScript 语言的基础,无需任何编程经验 npm install ...
随机推荐
- c#启动EXE文件(简单的)
在程序执行中会遇到启动本软件的exe问,或者启用其它的exe文件,已达到执行某些操作的作用.下面是两种最常见的启动exe文件. 1.调用系统dll使用其提供的方法. 引用的dll, [DllImpor ...
- MSM8909平台 LED背光的控制
之前齐师兄问我,是不是应该有一个文件记录背光灯的亮度,我说理论上有,但是在哪里我真的还没有见过.只知道在调LCD驱动的时候会调用一个背光控制的函数,传进来一个亮度值就可以配置亮度了,至于这个函数是谁调 ...
- 编写一个类A,该类创建的对象可以调用方法f输出小写的英文字母表。然 后再编写一个A类的子类B,要求子类B必须继承类A的方法f(不允许重写), 子类B创建的对象不仅可以调用方法f输出小写的英文字母表,而且可以调用子 类新增的方法g输出大写的英文字母表。最后编写主类C,在主类的main方法 中测试类A与类B。
package zimu; public class A { public void f() { for (int i = 97; i <123; i++) { System.out.print ...
- 【leetcode❤python】237. Delete Node in a Linked List
#-*- coding: UTF-8 -*- # Definition for singly-linked list.# class ListNode(object):# def __init ...
- UVA 1351 十三 String Compression
String Compression Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit ...
- [转]Socket send函数和recv函数详解
1.send 函数 int send( SOCKET s, const char FAR *buf, int len, int flags ); 不论是客户还是服务器应用程序都用send函数来向TCP ...
- The CLR's Execution Model
the native code generator tool:NGen.exe optimization tool:MPGO.exe 所有类型最终都继承自System.Object.则所有类型都有如下 ...
- zoj 1010 (线段相交判断+多边形求面积)
链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=10 Area Time Limit: 2 Seconds Mem ...
- So easy Webservice 7.CXF 发布WebService
(一)使用ServerFactoryBean 方式实现发布WS服务 1.新建项目,添加cxf jar包到项目中 2.编写服务实现类 /** * CXF WebService * 不用注解 * @aut ...
- python tools: iPython Notebook
Introducing IPython Notebook IPython isn't a different programming language, it's just a set of comp ...