Learnyounode (Complete "Filtered LS"), I am stuck!

Hi There, I am bit lost with step 19 (Complete “Filtered LS”) of learnyounode. I have 2 file (module and caller) but I don’t quite understand what the exercise required (about callback). Is there a way to give a peek to the solution to understand what I am doing wrong? Cheer.

What’s the solution you’ve tried so far?

Hi Guys, thank you both for following up.
Yes, I am actually in the next section (Make it modular).

I have spent little more time on the exercise (your diagram helped me to undnerstand what I was asked to accomplish), but still there’s something messy in my code.
Below my files.

Module:
var fs = require(‘fs’);
var path = require(‘path’);

// var dirLocation = process.argv[2];
// var fileExtension = process.argv[3];
var filteredDir = [];
// console.log(dirLocation + fileExtension);

function myFilteredRead(dirLocation, fileExtension, cb) {
fs.readdir(dirLocation, function (err, data) {
if (err) {
console.log(err);
cb(err);
return
}
data.forEach(function (file, index, data) {
if (path.extname(file) == “.” + fileExtension) {
// console.log(file);
filteredDir.push(file)
}
cb(null, filteredDir);
})
});
}

//myFilteredRead(process.argv[2], process.argv[3]);
module.exports = myFilteredRead;

Caller:
var filteredDir = require("./app6a");

function printDir(err, data){
// console.log(err);
// console.log(data);
if(err){
console.log(err);
return;
}

data.forEach(function (file, index, data) {
        console.log(file);
});

}

filteredDir(process.argv[2], process.argv[3], printDir);


I just started Freecodecamp few days ago, as a general feedback I would suggest to spend a bit more on theory before jumping into code. Spending a few words explaining how things works (behind the scene) would help to resonate more on problems and code.

Cheers, Giovanni

Hey P1xt (what kind of strange names are used in your country??!),
yeah using your code as guidance I managed to get mine working, thanks heaps.

I key change is this:
return cb(err);
// cb(err);

cb(null, filteredDir);

Can you tell me when why in one case you ‘return’ the callaback (<return cb(err)>) while in another case you just call the function directly? (<cb(null, filteredDir)>)

Thanks again and I’ll see you soon … (many more challenges before the end of the curriculum!)

Giovanni

Man, thanks for taking the time to respond.
I think I am hitting a limitation of this course/program. This is not for real beginners, it take for granted too many concepts.
I am really confused with object/variable/function/return. This is where a bit of an explanation is more powerful than few line of code (at least for me).
Does ‘return’ pass something from the function to the caller in this case an external program?
Does it return the result of the function ‘callback(err)’ or the object ‘callback’ all together?

…I’ll move to the next exercise and I see if it gets better.

Cheers, Giovanni

var sum = addTwoNumbers(2, 3); //returns 5

function addTwoNumbers(a, b) {
  return a + b;
}

A function’s return value is its output.

That’s cool, what about the difference between the 2 ‘return’ below?

function mathOperations(){
    function addTwoNumbers(a, b){
        return a + b;
    }
    var diff = subTwoNumbers(c, d){
        var result = c - d;
    }
    return diff;
}

Here is my solution:
` require(‘path’);
var fs = require(‘fs’);
var apath = process.argv[2].toString();
var dir = ‘.’+process.argv[3].toString();
var temp;

fs.readdir(apath, function callback(err, list) {
if (err){
console.log("Error: "+err.toString())
}

for (var i = 0; i < list.length; i++){
temp = list[i];
if (temp.indexOf(dir) != -1){
console.log(temp);
}

}

 });`

There’s not much of a difference, but you will only ever get the last return value because addTwoNumbers() never gets called. The function mathOperations() outputs a function, but that function only declares a variable and does nothing with it.

var myNewFunction = mathOperations();

console.log(myNewFunction); // outputs subTwoNumbers(c, d){
                                           var result = c - d;
                                         }

var output = myNewFunction(3, 2); //Doesn't return anything
console.log(output); //undefined

My solution is similar, except I’m not using the path package from node.

Here goes:

This is the main file (filterfileswithmodules.js):

(function main() {
  "use strict";
  var module_filterfiles = require("./modulefilterfiles.js");

  var directory = process.argv[2];
  var extension = process.argv[3];
  
  function callbackFunction(err, filteredFilesArr) {
    if (err) {
      console.log(err);
    } else {
      console.log(filteredFilesArr.join("\n"));
    }
  }
  
  module_filterfiles(directory, extension, callbackFunction); 
  
})();

Here is the module file (modulefilterfiles.js):

 module.exports = function filterfiles(dirname, extension, callbackFunction) {
  "use strict";
  
  var fs = require("fs");
  fs.readdir(dirname, callback);
  
  function callback(err, filenamesArr) {
    if (err) {
      return callbackFunction(err);
    }
    var filteredFilenamesArr = filenamesArr.filter(function filterextensions(filename) {
      return ( ((filename.indexOf(".") !== -1) && (filename.slice(filename.lastIndexOf(".")+1) === extension)) ? filename : "");
    });
    
    callbackFunction(null, filteredFilenamesArr);
  }
    
}

And this is how I run it:

> learnyounode run filterfileswithmodules.js

Two things I realized about myself on my JavaScript learninig journey:

  1. I always use “use strict”;
  2. I HATE anonymous functions (they make the code harder for me to read and debug) :grinning:

I have to admit I was stuck on this modules assignment (because I found the instructions difficult to understand. Only after reading @P1xt’s flowchart did I understand what the problem was asking of me.

I wonder if other people are having a hard time understanding the instructions too. If so, maybe FCC should make the instructions a little clearer for this problem.