nodeschool / discussions

:school::speech_balloon: need help with nodeschool? or just wanna ask a question? open an issue on this repo!
488 stars 107 forks source link

Make it modular #1470

Closed SigmaEva closed 8 years ago

SigmaEva commented 8 years ago

Hey all, i am getting a fail in learnyounode - make it modular. These are my results :

✓ Submission results match expected ✓ Additional module file exports a single function ✓ Additional module file exports a function that takes 3 arguments ✓ Additional module file handles errors properly ✓ Additional module file handles callback argument ✓ Additional module file returned two arguments on the callback function ✗ Your additional module file [mymodule.js] did not return an Array object as the second argument of the callback ✓ Additional module file returned two arguments on the callback function ✗ Your additional module file [mymodule.js] did not return an Array object as the second argument of the callback /usr/local/lib/node_modules/learnyounode/node_modules/workshopper-exercise/exercise.js:177 processors[i].call(self, mode, function (err, pass) { ^ TypeError: Cannot call method 'call' of undefined

And this is my code :

var mymodule = require('./mymodule.js');
var path = require('path');

mymodule(process.argv[2],process.argv[3],function(err,data){
    if(err)throw err;
        console.log(data);  
});

and the mymodule file

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

module.exports = function(directory,extention,callback){
    fs.readdir(directory, function (err, list) {
        if(err){
            return callback(err);
        }
        list.forEach(function (data) {
            if (path.extname(data) === '.' + extention){
                return callback(null,data);
            }
        })

    })
}

thanx in advnce :)

katjad commented 8 years ago

You are currently getting the correct results when you run the program, but there is one step missing to make the solution pass. In your module, the second argument of the callback is meant to be an array. At the moment you apply it to just an item of an array, as you have the callback inside the function of the forEach method.

What you need to do is to filter your list and run the callback with the filtered list as the second argument.

You will also have to change the callback in the original file that requires the module, as it will now take in an array.

Hope that helps

wuyun1 commented 8 years ago

code file:

var mymodule = require('./mymodule.js');
var path = require('path');

mymodule(process.argv[2],process.argv[3],function(err,list){
    if(err)throw err;
        console.log(list.join("\n"));  
});

module file:

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

module.exports = function(directory,extention,callback){
    fs.readdir(directory, function (err, list) {
        if(err){
            return callback(err);
        }
        var  filter_list=[];
        list.forEach(function (data) {
            if (path.extname(data) === '.' + extention){
                filter_list.push(data);
            }
        })
        return callback(null,filter_list);
    })
}
SigmaEva commented 8 years ago

sorry for the late reply :( thanx a lot to both of you!

SomeoneWeird commented 8 years ago

:tada: