Finds customer who live within 100km and output them to a text file I had an interview task where I had to output customers who live 100km within a particular latitude and longitude and then output them to a file with their username and ID. The customer file looks like the following:
{&quot;latitude&quot;: &quot;52.986375&quot;, &quot;user_id&quot;: 12, &quot;name&quot;: &quot;Christina McArdle&quot;, &quot;longitude&quot;: &quot;-6.043701&quot;}
{&quot;latitude&quot;: &quot;51.92893&quot;, &quot;user_id&quot;: 1, &quot;name&quot;: &quot;Alice Cahill&quot;, &quot;longitude&quot;: &quot;-10.27699&quot;}
{&quot;latitude&quot;: &quot;51.8856167&quot;, &quot;user_id&quot;: 2, &quot;name&quot;: &quot;Ian McArdle&quot;, &quot;longitude&quot;: &quot;-10.4240951&quot;}
{&quot;latitude&quot;: &quot;52.3191841&quot;, &quot;user_id&quot;: 3, &quot;name&quot;: &quot;Jack Enright&quot;, &quot;longitude&quot;: &quot;-8.5072391&quot;}

Here is the code to implement the calculation for the great-circle-distance
/**
 * solution accuired from
 * https://www.movable-type.co.uk/scripts/latlong.html
 */

const greatCircleDistanceCalc = (lat1, lon1, lat2, lon2) =&gt; {
  const R = 6371; // Radius of the earth in km
  const dLat = deg2rad(lat2 - lat1); // deg2rad below
  const dLon = deg2rad(lon2 - lon1);
  const a =
    Math.sin(dLat / 2) * Math.sin(dLat / 2) +
    Math.cos(deg2rad(lat1)) *
      Math.cos(deg2rad(lat2)) *
      Math.sin(dLon / 2) *
      Math.sin(dLon / 2);
  const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
  const d = R * c; // Distance in km
  return d;
};

const deg2rad = (deg) =&gt; {
  return deg * (Math.PI / 180);
};

module.exports = {
    greatCircleDistanceCalc,
}; 

Here is the main app.js where the calculation is used and a customer file is read and a new file is outputted.
const fs = require(&quot;fs&quot;);
const { greatCircleDistanceCalc } = require(&quot;./greatCircleDistanceCalc.js&quot;);

const customerFile = fs.readFileSync(&quot;./customers.txt&quot;, &quot;utf-8&quot;); //Read in file
const customerArr = customerFile.split(&quot;\n&quot;).map((s) =&gt; JSON.parse(s)); //Convert file into array of objects

const dublinOffice = {
  latitude: &quot;53.339428&quot;,
  longitude: &quot;-6.257664&quot;,
};

const invitedArr = [];

//Sort data in ascending order
const sortedCustomers = customerArr.sort((a, b) =&gt; {
  return a.user_id - b.user_id;
});

const closestCustomers = (sortedCustomers, arr) =&gt; {
  for ({ latitude, longitude, user_id, name } of sortedCustomers) {
    if (
      greatCircleDistanceCalc(
        dublinOffice.latitude,
        dublinOffice.longitude,
        latitude,
        longitude
      ) &lt;= 100
    ) {
      invitedArr.push(`${name}:${user_id}`);
    }
  }
  writeInvitedCustomer(arr);
};

const writeInvitedCustomer = (arr) =&gt; {
  const writeStream = fs.createWriteStream(&quot;Output.txt&quot;);
  const pathName = writeStream.path;

  arr.forEach((value) =&gt; writeStream.write(`${value}\n`));
  writeStream.on(&quot;finish&quot;, () =&gt; {
    console.log(`wrote all the array data to file ${pathName}`);
  });

  writeStream.on(&quot;error&quot;, (err) =&gt; {
    console.error(`There is an error writing the file ${pathName} =&gt; ${err}`);
  });

  writeStream.end();
};

closestCustomers(sortedCustomers, invitedArr);

Overall I am happy with the solution but I think the only thing is I am not sure how to write a test for this.
As always any feedback and suggestions are much welcome.
Generally I find the code very unstructured making reading it a bit confusing:
First you execute some code outside of a function to read the data from a file, then you define a constant (dublinOffice), then you define a variable (invitedArr), but don't use it in the next block of code, then you sort the data, then you define two functions, and finally execute one of the functions.
Further more the function closestCustomers both gets invitedArr passed an argument (and uses it as such) and uses it directly as a global variable.
Personally I'd have the main code calling functions first (after defining constants) and then define all functions at the end of the source code using function so that they are hoisted. Other people may define the functions first and then use them, but that is a matter of taste, as long it's not all mixed up.
More, smaller functions would also be helpful. Reading the data should be in its own function and closestCustomers does too much (filtering, converting the objects to strings, and calling writeInvitedCustomer). This would also make testing easier.
Using the array methods filter() and map() would help the code be more readable. Personally, I'd start with something like:
const inputFileName = &quot;customers.txt&quot;;
const outputFileName = &quot;output.txt&quot;;

const dublinOffice = {
  latitude: &quot;53.339428&quot;,
  longitude: &quot;-6.257664&quot;,
};
const maxDistance = 100;

const invitedCustomers = readCustomers(inputFileName)
  .filter(withinKms(dublinOffice, maxDistance ))
  .sort(userIdComparator)
  .map(formatCustomerForOutput); // `map` may be considered to be moved inside `writeInvitedCustomers`

writeInvitedCustomers(invitedCustomers, outputFileName);


function readCustomers(filename) {
  // ...
}

function withinKms(location, kms) {
   return (customer) =&gt; greatCircleDistanceCalc(customer.latitude, customer.longitude, location.latitude, location.longitude) &lt;= kms;
}

function userIdComparator({user_id: userId1}, {user_id: userId2}) {
   return userId1 - userId2;
}

function formatCustomerForOutput({name, user_id}) {
   return `${name}:${user_id}`;
}

// etc.


One other point: I'm not familiar with node's WriteStream, but it looks wrong for me to assign the event handlers after using it by calling its write method. Can't write cause an error?
NB: This is probably not your choice, if this was an interview task, but I find the file format for customers.txt (multiple JOSN objects in one file) strange. A straight forward JSON array would make more sense.