Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 关于学习的问题Younode练习5_Javascript_Node.js - Fatal编程技术网

Javascript 关于学习的问题Younode练习5

Javascript 关于学习的问题Younode练习5,javascript,node.js,Javascript,Node.js,大家好,我正在努力解决nodeschool的练习,我从LearnyYounode开始,很多人推荐初学者使用它,练习问题是: # LEARN YOU THE NODE.JS FOR MUCH WIN! ## FILTERED LS (Exercise 5 of 13) Create a program that prints a list of files in a given directory, filtered by the extension of the files. You w

大家好,我正在努力解决nodeschool的练习,我从LearnyYounode开始,很多人推荐初学者使用它,练习问题是:

# LEARN YOU THE NODE.JS FOR MUCH WIN!

## FILTERED LS (Exercise 5 of 13)

 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. Note that the second
 argument will not come prefixed with a '.'.

 Keep in mind that the first arguments of your program are not the first
 values of the process.argv array, as the first two values are reserved for
 system info by Node.

 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 callback (err, list) { /* ... */ }

 where list is an array of filename strings.

 Documentation on the fs module can be found by pointing your browser here:
 file://C:\Users\Filipe\AppData\Roaming\npm\node_modules\learnyounode\node_
 apidoc\fs.html

 You may also find node's path module helpful, particularly the extname
 method.

 Documentation on the path module can be found by pointing your browser
 here:
 file://C:\Users\Filipe\AppData\Roaming\npm\node_modules\learnyounode\node_
 apidoc\path.html

─────────────────────────────────────────────────────────────────────────────


  » To print these instructions again, run: learnyounode print

  » To execute your program in a test environment, run: learnyounode run

    program.js

  » To verify your program, run: learnyounode verify program.js

  » For help run: learnyounode help
我试着解决这个练习:

var fs = require('fs');

fs.readdir(process.argv[2],process.argv[3],function(err,list){
    for(var i = 0;i<list.length;i++)
    {
        var fileParts = list[i].split(".");
        if(fileParts[1] == process.argv[3]){
            console.log(list[i] + "\n");
        }
    }
});
var fs=require('fs');
fs.readdir(process.argv[2],process.argv[3],函数(err,list){

对于(var i=0;iHm),我刚刚测试了您的代码,它实际上对我来说运行良好。使用已解决的验证返回。不确定您正在经历什么。顺便说一句:至少进行一些错误处理。下面是我的解决方案:

const fs = require('fs');
const test = '.' + process.argv[3]

fs.readdir(process.argv[2], (err, data) => {
    if (err) { 
        console.error(err); 
    } else {
        data.filter(file => {
            if (file.substring(file.length - test.length) === test) {
                console.log(file)
            }
        })  
    }
});
“它不工作”是什么意思?你收到错误消息了吗?你的程序输出了什么?