Author: Christopher Novelli Language: javascript
Description: Lifion Developer - Case Study - Algorithm Solution Timestamp: 2018-04-09 04:14:40 +0000
View raw paste Reply
// Dummy list of subscribers
const subscriberList = [
  {accountNum: 2018, firstName: "Luke", lastName: "Skywalker"},
  {accountNum: 2019, firstName: "Anakin", lastName: "Skywalker"},
  {accountNum: 2020, firstName: "Shmi", lastName: "Skywalker"},
]

//Dummy list of invoices
const invoiceList = [
  {invoiceNum: 12346, accountNum: 2018, paid: false},
  {invoiceNum: 12345, accountNum: 2019, paid: true},
  {invoiceNum: 12347, accountNum: 2020, paid: false},
]

function paidOrNot(invoices, subscribers) {
 
  // declare constant lengths to be utilized in for/forEach loops
  // So as to no calculate this value each time loop is run
  // Potentially worthwhile optimization, for very large data sets
  const invoicesLength = invoices.length;
  const subscribersLength = subscribers.length;
  var unpaidAccountNum = 0;
 
  //Loop through invoice list to see which invoices are unpaid
  invoices.forEach(function(invoice) {
    //If invoice is unpaid, keep track of associated account number
     if(invoice.paid == false) {
      unpaidAccountNum = invoice.accountNum;
    //Only if unpaid, loop through subscribers list
    //Unecessary otherwise, for the specific purpose of this function
    subscribers.forEach(function(subscriber) {
      // Attempt to match unpaid invoice account number with subscriber
      // If found, send cancellation notice
      if(unpaidAccountNum == subscriber.accountNum) {
        // This is serving as a model for the cancellation notice process
        // Would likely call custom message / subscriber notification function
        // However, task only calls for a single function
         console.log(subscriber.firstName + " " + subscriber.lastName + ", your account is CANCELLED - that is why you fail!");
        }
      })
    }
  })
}
// Call function with dummy date, to test - Success!
paidOrNot(invoiceList, subscriberList);
View raw paste Reply