nodeschool learnyounode node.js module FILTER LS exercise

后端 未结 8 1284
面向向阳花
面向向阳花 2021-02-10 05:38

Below is the exercise 5 of nodeschool learnyounode module

Create a program that prints a list of files in a given directory, filtered by he extension of the files. You w

8条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-10 06:08

    You can try this code to solve this exercise :

    var fs = require('fs');
        function endsWith(str, suffix) {
            var s = str.slice(str.length - suffix.length - 1);
            if (s == "." + suffix)
                return true;
            else
                return false;
    };
    
    
    fs.readdir(process.argv[2], function (err, list) {
        if (process.argv[3]) {
            for (var i = 0; i < list.length; i++) {
                if (endsWith(list[i], process.argv[3]))
                    console.log(list[i]);
            }
        }
    });
    

提交回复
热议问题