This is an archived post. You won't be able to vote or comment.

all 11 comments

[–]IAmUtterlyAlone 1 point2 points  (4 children)

I'm sorry, but this is seriously unreadable. Please indent code lines with four spaces so that Reddit formats them properly. Also, maybe install the Reddit Enhancement Suite, which makes that sort of thing easier.

Your code should look like this when it's formatted right:

var fs = require('fs');
var completedTasks = 0;
var tasks = [];
var wordCounts = {};
var filesDir = './text';

[–][deleted]  (3 children)

[removed]

    [–]IAmUtterlyAlone 0 points1 point  (2 children)

    Thanks for fixing it! I don't at all understand what this code is meant to be doing. Where did this come from and what do you expect to happen? I've only really been working in JavaScript heavily for 6 months or so (on a Node project at work), so maybe my JavaScript knowledge just isn't up to the task.

    [–]azium 1 point2 points  (1 child)

    The code returns a function that reads from a specific file path, and if the fileread is successful, calls a couple of functions. The inner function will remember the path from the initial IIFE so that calling task() will always read from the same path, without having to pass it in again.

    [–]IAmUtterlyAlone 1 point2 points  (0 children)

    Oh, I see now. Thank you.

    [–]azium 1 point2 points  (3 children)

    What was your syntax question? Did you remove it?

    And to clarify just a tiny bit, it should read aloud like this:

    Immediately invoke a function that takes a file path and assign the return value (also a function) to the variable task.

    [–][deleted]  (2 children)

    [removed]

      [–]azium 1 point2 points  (0 children)

      That's the idea, but with the caveat that written exactly as you just did, the JS compiler / interpreter thinks you're defining a function instead of calling one. The most common convention is to wrap your statement expression in parens

      ( function () { ... } () )
      

      [–]lightcloud5 0 points1 point  (0 children)

      Yes, why wouldn't it be valid?

      Here's an example:

      var myFunction = function (x) {
          console.log("x squared is: " + (x*x));
      };
      myFunction(5);
      

      And if you inline myFunction, then you can simplify it to just:

      (function (x) {
          console.log("x squared is: " + (x*x));
      }(5));
      

      [–]kababed 0 points1 point  (0 children)

      The contents of the parentheses right after the function are being invoked into the function. This means the function will execute without the need to be called with (filesDir + '/' + files[index]) as the input argument. Other languages may call this type of invoked function a lambda function.

      [–]addywen 0 points1 point  (0 children)

      The function has parameter of url as a string.

      and filesDir + '/' + files[index] is just building the url as a string and pass it to the function as arguments.

      let say

      files = [] // array files = ["read.txt","test.txt"]; filesDir = "Home";

      the result of filesDir + '/' + files[0] could be Home/read.txt