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 ...
随机推荐
- .Net使用微軟自帶的用戶驗證和登錄授權
使用微軟的用戶驗證,權限管理的方法 一.使用配置好的數據庫, 1.首先建立想定的數據庫(只填寫名字不加載任何表,如:Login數據庫) 2.使用vs兼容工具命令提示.如圖: 3.導入數據庫所需要的ta ...
- jquery之event与originalEvent的关系、event事件对象用法浅析
在jquery中,最终传入事件处理程序的 event 其实已经被 jQuery 做过标准化处理, 其原有的事件对象则被保存于 event 对象的 originalEvent 属性之中, 每个 even ...
- HTMl中Meta标签详解以及meta property=og标签含义
meta是用来在HTML文档中模拟HTTP协议的响应头报文.META标签是HTML语言HEAD区的一个辅助性标签,它位于HTML文档头部的<HEAD>标记和<TITLE>标记之 ...
- 《安全智库》:48H急速夺旗大战通关writeup(通关策略)
作者:ByStudent 题目名字 题目分值 地址 MallBuilder2 350 mall.anquanbao.com.cn MallBuilder1 200 mall.anquanbao.c ...
- HTTP Status 404–/webDemo/hello
现在用一排很小的字写出来,我真是个大傻逼
- kaili 2.0 虚拟机修改ip
第一步:将虚拟机设置为桥接状态,并勾选上复制物理网络连接状态
- HttpServletResponse 学习
1: 利用Response向浏览器输出中文: private void test1(HttpServletResponse response) throws IOException { String ...
- 【转】《深入理解计算机系统》C程序中常见的内存操作有关的典型编程错误
原文地址:http://blog.csdn.net/slvher/article/details/9150597 对C/C++程序员来说,内存管理是个不小的挑战,绝对值得慎之又慎,否则让由上万行代码构 ...
- .net 连接sqlserver类库
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Da ...
- Java中JTree的简单案例
package ch12; import javax.swing.*; import javax.swing.tree.DefaultMutableTreeNode; /** * Created by ...