Tuesday, 25 April 2017

why is undefined being added to my filtered list?

I'm trying to make write a node program that will read a file director and filter out those without a certain file extension. (This is a challenge for learnyounode) For some reason it adds to the list 'undefined'. Can anyone tell me why?

var fs = require('fs')

var path = process.argv[2];
var ext = process.argv[3];

var fileList = fs.readdir(path, function callback(err, list){
    if (err){
    throw err;
    }
    var filteredList = list.filter(function(fileName){  
    var extRx = new RegExp('\.' + 'md' + '$');
        return extRx.test(fileName);
    });
    console.log(filteredList.forEach(function(val){console.log(val)}));
});

Outputs:

             ACTUAL                                 EXPECTED                

   "CHANGELOG.md"                      ==    "CHANGELOG.md"                     
   "LICENCE.md"                        ==    "LICENCE.md"                       
   "README.md"                         ==    "README.md"                        
   "undefined"                         !=    ""                                 
   ""                                  !=                                       



via David J.

No comments:

Post a Comment