'MAKE IT MODULAR' not getting marked '[COMPLETED]'
My code seems to be passing:
$ learnyounode verify modular.js
Verifying "MAKE IT MODULAR"...
ACTUAL EXPECTED
------ --------
"CHANGELOG.md" "CHANGELOG.md"
"LICENCE.md" "LICENCE.md"
"README.md" "README.md"
"" ""
[Error: testing]
but 'MAKE IT MODULAR' is not getting marked '[COMPLETED]' in the main menu.
Here is my code:
//filterModule.js
var fs = require('fs');
module.exports = function(path, filter) {
var regex = new RegExp("\\." + process.argv[3] +"$");
fs.readdir(path, function(err, data) {
if (err) {
return console.log(err)
}
// print a list of files path, with the extention given
for (var i = 0; i < data.length; i++) {
if (regex.test(data[i])) {
console.log(data[i])
}
};
})
}
//modular.js
var filter = require('./filterModule.js');
filter(process.argv[2], process.argv[3]);
OK I should have looked at the solutions in the repo first - I see that although I was generating the right output, I wasn't doing the callback correctly. Maybe there is an error that could be thrown for that?
no, you shouldn't have looked at the solutions first, cheater!
thanks for filing this issue though because it's reminded me that I've been meaning to flesh out the verification of that exercise more so that you have to get the module just right. Version 0.2.14 now checks the signature and that you fire the callback with an argument when one happens and with an array of files when you don't have an error. It ought to be pretty thorough now and give some good guidance when you don't get it quite right.
Upgrade and try it against the solution you pasted above and you should see that it points you in the right direction.
Haha! Great! I'll revert to my own code and check it out. On 22 Sep 2013 00:15, "Rod Vagg" [email protected] wrote:
no, you shouldn't have looked at the solutions first, cheater!
thanks for filing this issue though because it's reminded me that I've been meaning to flesh out the verification of that exercise more so that you have to get the module just right. Version 0.2.14 now checks the signature and that you fire the callback with an argument when one happens and with an array of files when you don't have an error. It ought to be pretty thorough now and give some good guidance when you don't get it quite right.
Upgrade and try it against the solution you pasted above and you should see that it points you in the right direction.
— Reply to this email directly or view it on GitHubhttps://github.com/rvagg/learnyounode/issues/24#issuecomment-24872547 .
I get the same error but my solution is different. this isn't modular?
module.exports = function(dir,filter){ var fs = require("fs"); var format=filter; fs.readdir(dir,function(err,list){ if(err) return callback(err);
for(var i =0 ; i <list.length;i++){
if(list[i].split(".")[1]==format)
console.log(list[i]);
}
});
}
if you have an up-to-date version of learnyounode then it should be telling you why this isn't quite correct. Run npm install learnyounode -g to update.
Basically, the problem asks for a function that takes 3 arguments, the last being a callback, and the callback needs to get an array of files; your module shouldn't be printing anything to stdout.
This one is still broken. I'm getting "did not return an Array with the correct number of elements as the second argument of the callback" I installed learnyounode this morning. So it's up to date.
******** program.js ************************************************************************************ var module = require('./module'); var pattern = '.' + process.argv[3] + '$';
module(process.argv[2], pattern, function (err, list) {
if (err != null) {
console.log(err);
}
list.forEach(function (item, i) {
console.log(item);
});
});
******** module.js ************************************************************************************** module.exports = function (directory, filter, callback) { var fs = require('fs'); //console.log(process.argv[2]); fs.readdir(directory, function (err, files) {
if (err != null) {
return callback(err);
}
var exp = new RegExp(filter);
files = files.filter(function (item) {
return exp.test(item);
});
callback(null, files);
});
}
You have not escaped the backslash in your program.js. Try:
var pattern = '\\.' + process.argv[3] + '$';
ref: https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions
Hi Thanks for the response. But I'm still getting the same result:

I SEE WHAT I DID WRONG... read bottom
MattBH, it doesn't look like there is a pattern check in your code. I'm thinking maybe my issue has to do with the fact I'm using a substring check instead of regex. Didn't seem to be an issue on the previous tutorial though.
Odd though, and perhaps now I get it. It says "Did not return return an array with the correct number of elements AS THE SECOND ARGUMENT IN THE CALL BACK" I'm sorry, but the callback will NOT have the same number of elements because the call back is the only component that can be responsible for handling the filtering of file types.

I had a conceptual gap. I put the filter and the printing together. The tutorial wants you to do them separately.

Thanks Keysle. It just prevents that nice all completed feeling... Even though the output looks the same.
On Sun, Dec 15, 2013 at 8:16 AM, Keysle [email protected] wrote:
MattBH, it doesn't look like there is a pattern check in your code. I'm thinking maybe my issue has to do with the fact I'm using a substring check instead of regex. Didn't seem to be an issue on the previous tutorial though.
Odd though, and perhaps now I get it. It says "Did not return return an array with the correct number of elements AS THE SECOND ARGUMENT IN THE CALL BACK" I'm sorry, but the callback will NOT have the same number of elements because the call back is the only component that can be responsible for handling the filtering of file types.
[image: image]https://f.cloud.github.com/assets/598169/1749896/c0aa030c-654f-11e3-8122-13abec992b16.png
— Reply to this email directly or view it on GitHubhttps://github.com/rvagg/learnyounode/issues/24#issuecomment-30599078 .
Thank you for this. I couldn't for the life of me understand the instructions.