The following code cannot realize the corresponding function, please take a look

  node.js, question

This code is to count the words in each text file in the text directory. The code has no syntax error, but it cannot realize the corresponding function under the command line.

var fs = require('fs');
 var completedTasks = 0;
 var tasks = [];
 var wordCounts = {};
 var fileDir = './text';
 
 function checkIfComplete() {
 CompletedTasks plus;
 if (completedTasks == tasks.length) {
 for (var index in wordCounts) {
 Log (index plus':' plus wordCounts[index]);
 bracket
 bracket
 bracket
 
 function countWordsText(text) {
 Varwords = text.tostring (). to lower case (). split (\ w plus /).sort ();
 for (var index in words) {
 var word = words[index];
 if (word) {
 wordCounts[word] = (wordCounts[word]) ?  WordCounts[word] plus 1: 1;
 bracket
 bracket
 bracket
 
 fs.readdir(fileDir, function (err, files) {
 if (err) {
 throw err;
 bracket
 for (var index in files) {
 var task = (function (file) {
 return function () {
 fs.readFile(file, function (err, text) {
 countWordsText(text);
 checkIfComplete();
 });
 bracket
 })(fileDir plus'/'plus files[index]);
 bracket
 for (var task in tasks) {
 tasks[task]();
 bracket
 });

图片描述

Varwords = text.tostring (). to lower case (). split (\ w plus /).sort ();
 console.log(words);

==>

Varwords = text.tostring (). to lower case (). split (\ s plus /).sort ();
 console.log(words);

The following code should also be corrected

fs.readdir(fileDir, function (err, files) {
 if (err) {
 throw err;
 bracket
 for (var index in files) {
 var task = (function (file) {
 return function () {
 fs.readFile(file, function (err, text) {
 countWordsText(text);
 checkIfComplete();
 });
 bracket
 })(fileDir plus'/'plus files[index]);
 //Add task to the array, otherwise the length of tasks is 0, of course there is no result
 tasks.push(task);
 bracket
 for (var task in tasks) {
 tasks[task]();
 bracket
 });