Build a Device Loan Ledger - Build a Device Loan Ledger Test 18

Tell us what’s happening:

I can’t seem to get it to pass test 18 even though I do seem to be getting the desired results. Is there something I’m missing here? I even went a bit overboard on trying to ensure only those were getting into the array in the first place.

Your code so far

const equipmentLedger = {
  "1": { 
    type: "PC", 
    status: "CheckedOut", 
    borrower: { 
      name: "John Smith", 
      email: "john@acme.org" 
    }, 
    dueDate: "11/30/2025"
  },
  "2": { type: "Laptop", status: "CheckedIn", borrower: { name: "", email: "" }, dueDate: "" },
  "3": { type: "Laptop", status: "CheckedOut", borrower: { name: "Jane Doe", email: "jane@acme.org" }, dueDate: "10/31/2025" },
  "4": { type: "iPad", status: "CheckedIn", borrower: { name: "", email: "" }, dueDate: "" }
};

function checkoutDevice(ledger, assetTag, borrower) {
  if (!ledger[assetTag]) {
    return{ledger: ledger, message: assetTag + " not found"}
  }
  let updatedLedger = loadLedger(serializeLedger(ledger));
  if (updatedLedger[assetTag].status === "CheckedOut") {
    return {ledger: ledger, message: `Asset tag ${assetTag} already checked out`}
  } else {
  updatedLedger[assetTag].borrower.name = borrower.name;
  updatedLedger[assetTag].borrower.email = borrower.email;
  updatedLedger[assetTag].status = "CheckedOut";
  let confirmationString = `${assetTag} updated to ${borrower.name}`
  return {ledger: updatedLedger, message: confirmationString}
}}

function checkinDevice(ledger, assetTag) {
  if (!ledger[assetTag]) {
    return {ledger: ledger, message: `${assetTag} not found`};
  }
  const updatedLedger = loadLedger(serializeLedger(ledger));
  updatedLedger[assetTag].borrower.name = "";
  updatedLedger[assetTag].borrower.email = "";
  updatedLedger[assetTag].status = "CheckedIn";
  updatedLedger[assetTag].dueDate = "";
  const confirmationString = `${assetTag} updated`
  return { ledger: updatedLedger, message: confirmationString }
}

function listOverdueDevices(ledger, today) {

  let updatedLedger = loadLedger(serializeLedger(ledger));

  let results = [];
  let unsortedOverdueArray = []
  for (let i = 1; updatedLedger[i] !== undefined; i++) {
    if (updatedLedger[i].status === "CheckedOut" && updatedLedger[i].dueDate !== "") {
      let dueDateArray = updatedLedger[i].dueDate.split('/');
      if (dueDateArray[0].length === 1) {
        dueDateArray[0] = `0${dueDateArray[0]}`;
      }
      if (dueDateArray[1].length === 1) {
        dueDateArray[1] = `0${dueDateArray[1]}`;
      }
      const dueDateString = dueDateArray[2] + dueDateArray[0] + dueDateArray[1];

      if (updatedLedger[i].status === "CheckedOut") {
        unsortedOverdueArray.push([dueDateString, i])      
      }
    }
  }
    let sorted = unsortedOverdueArray.sort();
    for (let overdue of sorted) {
      if (updatedLedger[overdue[1]].status === "CheckedOut") {
      results.push(updatedLedger[overdue[1]])
      }
    }
  return results;
}

function serializeLedger (ledger) {
  return JSON.stringify(ledger)
}
function loadLedger(object) {
  return JSON.parse(object)
}

console.log(listOverdueDevices(equipmentLedger, '10/5/2026'))

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/154.0.0.0 Safari/537.36 Edg/154.0.0.0

Challenge Information:

Build a Device Loan Ledger - Build a Device Loan Ledger

GitHub Link: freeCodeCamp/curriculum/challenges/english/blocks/lab-device-loan-ledger/69f0a37c3bf194a7c9c62492.md at main · freeCodeCamp/freeCodeCamp · GitHub

Welcome to the forum @SomeAlienLow

Try adding the following console log:

console.log(listOverdueDevices(equipmentLedger, '10/5/2024'))

Is the list what you expected?

Happy coding

Ha! You know, I had made some logic for that at some point, and then in frustration and going up and down the page adjusting, I must’ve knocked that right out… I wasn’t even utilizing the “today” parameter by the time I sent this… Thanks a bunch! Good nudge without answering it for me by the way!