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 ...
随机推荐
- Python 字符串、元组、字典转换成列表
- HDU 4441 Queue Sequence(splay)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4441 题意:一个数列,三种操作:(1)插入:找到没在当前数列中的最小的正整数i,将其插在位置p之后,并 ...
- jquery append()详解
1 http://www.365mini.com/page/jquery-append.htm 2 http://blog.csdn.net/chaiyining007/article/details ...
- 粒子群优化算法(Particle Swarm Optimization)
粒子群算法的思想源于对鸟/鱼群捕食行为的研究,模拟鸟集群飞行觅食的行为,鸟之间通过集体的协作使群体达到最优目的,是一种基于Swarm Intelligence的优化方法.它没有遗传算法的"交 ...
- Linux系统下如何配置SSH?如何开启SSH
查询\安装SSH服务 1.登陆linux系统,打开终端命令.输入 rpm -qa |grep ssh 查找当前系统是否已经安装 2.如果没有安装SSH软件包,可以通过yum 或rpm安装包进行安装( ...
- CSRF的防御实例(PHP)
CSRF的防御可以从服务端和客户端两方面着手,防御效果是从服务端着手效果比较好,现在一般的CSRF防御也都在服务端进行. 1.服务端进行CSRF防御 服务端的CSRF方式方法很多样,但总的思想都是一致 ...
- Linux Shell脚本攻略 读书笔记
Linux Shell脚本攻略 读书笔记 这是一本小书,总共253页,但内容却很丰富,书中的示例小巧而实用,对我这样总是在shell门前徘徊的人来说真是如获至宝:最有价值的当属文本处理,对这块我单独整 ...
- 问题处理:找不到Pch预编译文件?
提醒:Xcode6之后就不再自动创建Pch预编译文件 在Xcode6之前,创建一个新工程xcode会在Supporting files文件夹下面自动创建一个“工程名-Prefix.pch”文件,也是一 ...
- codevs 1294 全排列 next_permuntation
#include<bits/stdc++.h> using namespace std; #define ll long long #define pi (4*atan(1.0)) #de ...
- js文件的装载和执行
1.浏览器对script引用的js文件分两步,下载,下载完毕后马上执行:这两步都会阻塞浏览器继续解析. 2.加入defer属性,<script defer type="text/jav ...