-1

I wrote a program that's supposed to output all the names of files having a defined extension in a defined folder, however when I add the following line:

list = list.filter(file => { file !== fileExtension });

Which I want to filter the files which name matches an extension name (A file called "txt" for example), I get not output.

Here is the complete code (I use node JavaScript to run it):

const fs = require('fs');

const dirPath = process.argv[2];
const fileExtension = process.argv[3];

fs.readdir(dirPath, (err, list) => {
    if (err) {
        return console.log('An error occurred while reading directory: ' + err);
    }  

    list = list.filter(file => { file !== fileExtension }); // No output when I add this line

    list = list.filter(file => file.split('.')[file.split('.').length - 1] === fileExtension);

    list.forEach((file) => {  
        console.log(file);
    });

});
Cerbrus
  • 70,800
  • 18
  • 132
  • 147
Platus
  • 1,753
  • 8
  • 25
  • 51

1 Answers1

1

you need to return the boolean:

list = list.filter(file => { return file !== fileExtension });

or

list = list.filter(file => file !== fileExtension);
XPX-Gloom
  • 601
  • 5
  • 11