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

Test Doesn't Recognize Syntactically Different Idiomatic 'Error First' Pattern on 'Make It Modular' #716

Closed manavm1990 closed 3 years ago

manavm1990 commented 3 years ago

Below, kindly see My Solution and LMK what is the issue with this one in comparison to 'Official' Solution. They seem very similar. πŸ˜–

My Solution

'make-it-modular.js'

const filterFiles = require("./mymodule");

const [_, __, dir, ext] = process.argv;

filterFiles(dir, ext, (err, files) => {
  if (err) {
    return console.error(err);
  }

  return files.forEach((file) => {
    console.info(file);
  });
});

'mymodule.js'

const { readdir } = require("fs");
const { extname } = require("path");

module.exports = (dir, ext, cb) => {
  readdir(dir, (err, files) => {
    if (err) {
      return cb(err);
    }

    return cb(
      null,
      files.filter((file) => extname(file) === `.${ext}`)
    );
  });
};
βœ“

 Submission results match expected

 βœ“

 Additional module file exports a single function

 βœ“

 Additional module file exports a function that takes 3 arguments

 βœ—

 Your additional module file [mymodule.js] does not appear to pass back an
 error received from fs.readdir(). Use the following idiomatic Node.js
 pattern inside your callback to fs.readdir(): if (err) return
 callback(err)

'Official' Solution

 Here's the official solution in case you want to compare notes:

─────────────────────────────────────────────────────────────────────────────
 _/usr/local/lib/node_modules/learnyounode/exercises/make_it_modular/soluti
 on/solution.js_ :

    'use strict'
    const filterFn = require('./solution_filter.js')
    const dir = process.argv[2]
    const filterStr = process.argv[3]

    filterFn(dir, filterStr, function (err, list) {
      if (err) {
        return console.error('There was an error:', err)
      }

      list.forEach(function (file) {
        console.log(file)
      })
    })

─────────────────────────────────────────────────────────────────────────────
 _/usr/local/lib/node_modules/learnyounode/exercises/make_it_modular/soluti
 on/solution_filter.js_ :

    'use strict'
    const fs = require('fs')
    const path = require('path')

    module.exports = function (dir, filterStr, callback) {
      fs.readdir(dir, function (err, list) {
        if (err) {
          return callback(err)
        }

        list = list.filter(function (file) {
          return path.extname(file) === '.' + filterStr
        })

        callback(null, list)
      })
    }
manavm1990 commented 3 years ago

Duplicate of 700 βœ