workshopper / learnyounode

Learn You The Node.js For Much Win! An intro to Node.js via a set of self-guided workshops.
Other
7.25k stars 1.84k forks source link

Bug of Make it modular #508

Closed ChanJuiHuang closed 7 years ago

ChanJuiHuang commented 7 years ago

This is my module that is extSearch.js.

var fs = require('fs');
var path = require('path');

module.exports = function(folderPath, ext, callback){
    ext = '.' + ext;    
    fs.readdir(folderPath, 'utf8', function(err, files){
        if (err){
            callback(err, null);
        }
        else{
            let outputFiles = [];
            for (let i = 0; i < files.length; i++){       
                if (path.extname(files[i]) === ext){                
                    outputFiles.push(files[i]);
                }
            }
            callback(err, outputFiles);
        }        
    });    
};

In this line, fs.readdir(folderPath, 'utf8', function(err, files){...}, I add option of 'utf8' then learnyounode's feedback is FAIL Your solution to MAKE IT MODULAR didn't pass. Try again!

However, I erase the 'utf8' and change code to fs.readdir(folderPath, function(err, files){...} then I pass all test!

This is my program.js.

var extSearch = require('./extSearch.js');
var folderPath = process.argv[2];
var ext = process.argv[3];

extSearch(folderPath, ext, function(err, files){
    if (err){
        console.log(err);
    }
    else{
        for (let i of files){
            console.log(i);
        }
    }    
});
martinheidegger commented 7 years ago

Closed by #502