Build a Device Loan Ledger - Build a Device Loan Ledger

Tell us what’s happening:

I can’t get tests #21 and #22 to pass. I’ve checked my listOverdueDevices function using several different dates, and I’m getting the expected results, but it keeps saying my list is not in ascending order even though it is. I’m not sure what else to try.

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 serializeLedger(ledger) {
  let json = JSON.stringify(ledger);
  return json;
}

function loadLedger(json) {
  let convertedLedger = JSON.parse(json);
  return convertedLedger;
}

function checkoutDevice(ledger, assetTag, borrower) {
  const updatedLedger = loadLedger (serializeLedger (ledger));
  let confirmationString;
  if (Object.hasOwn(updatedLedger, assetTag) === false) {
    confirmationString = `Asset ${assetTag} does not exist in the system.`;
  } else if (updatedLedger[assetTag].status === "CheckedOut") {
    confirmationString = `Asset ${assetTag} is already checked out.`;
  } else if (Object.hasOwn(updatedLedger, assetTag) === true) {
    
    updatedLedger[assetTag].borrower.name = borrower.name;
    updatedLedger[assetTag].borrower.email = borrower.email;
    updatedLedger[assetTag].status = "CheckedOut";
    confirmationString = `${borrower.name} has successfully checked out ${assetTag}.`;
    
  } 
  return { ledger: updatedLedger, message: confirmationString };
}

function checkinDevice(ledger, assetTag) {
  const updatedLedger = loadLedger (serializeLedger (ledger));
  let confirmationString;
  if (Object.hasOwn(updatedLedger, assetTag) === false) {
    confirmationString = `Asset ${assetTag} does not exist in the system.`;
  } else {
    updatedLedger[assetTag].borrower.name = "";
    updatedLedger[assetTag].borrower.email = "";
    updatedLedger[assetTag].dueDate = "";
    updatedLedger[assetTag].status = "CheckedIn";
    confirmationString = `Asset ${assetTag} has been checked in.`;
  }
  return { ledger: updatedLedger, message: confirmationString };
}


function listOverdueDevices(ledger, today) {
  let cloneLedger = loadLedger(serializeLedger(ledger));
  let todayArr = today.split("/");
  let dueDateIndexArr = [];
  let compareArr = [];
  let dueDatesStrArr = [];
  let sortArr = [];
  let overdueDevices = [];

//formatting todayArr

  if (todayArr[0].length === 1) {
    todayArr[0] = `0${todayArr[0]}`;
  }

  if (todayArr[1].length === 1) {
    todayArr[1] = `0${todayArr[1]}`;
  }

  let todayStr = todayArr[2] + todayArr[0] + todayArr[1];

//  console.log(todayStr);

//puts due dates into an array

  for (let i = 1; i < Object.keys(cloneLedger).length; i++) {
    if (cloneLedger[i].status === "CheckedOut") {
      dueDateIndexArr.push(cloneLedger[i]);
    }
  }

//splits individual due dates
  for (let date of dueDateIndexArr) {
    let dateArr = date.dueDate.split("/");

//formats due dates

    if (dateArr[0].length === 1) {
      dateArr[0] = `0${dateArr[0]}`;
    }

    if (dateArr[1].length === 1) {
      dateArr[1] = `0${dateArr[1]}`;
    }

    compareArr.push(dateArr);
    let dueDateStr = dateArr[2] + dateArr[0] + dateArr[1];

    dueDatesStrArr.push(dueDateStr);

  }

 // console.log(compareArr);
//  console.log(dueDatesStrArr);
  

  //compare dates in array with today's date

  for (let i = 0; i < dueDatesStrArr.length; i++) {
    if (dueDatesStrArr[i] < todayStr) {
      sortArr.push(compareArr[i]);
    }  
  }
  
//  console.log(sortArr);

if (sortArr.length > 1) {
  for (let i = 0; i < sortArr.length - 1; i++) {
    if (sortArr.length > 1 && sortArr[i][2] > sortArr[i + 1][2]) {
      overdueDevices.push(dueDateIndexArr[i + 1], dueDateIndexArr[i]); 
    } else if (sortArr.length > 1 && sortArr[i][2] === sortArr[i + 1][2] && sortArr[i][0] > sortArr[i + 1][0]) {
      overdueDevices.push(dueDateIndexArr[i + 1], dueDateIndexArr[i]);
    } else if (sortArr.length > 1 && sortArr[i][0] === sortArr[i + 1][0] && sortArr[i][1] > sortArr[i + 1][1]) {
      overdueDevices.push(dueDateIndexArr[i + 1], dueDateIndexArr[i]);
    }
  }
} else {
   for (let i = 0; i < sortArr.length; i++) {
    if (sortArr.length === 1) {
      overdueDevices.push(dueDateIndexArr[i]); 
    } 
  }   
    } 
  if (overdueDevices !== []) { 
  return overdueDevices;
  }
  }

console.log(listOverdueDevices(equipmentLedger, "11/5/2024"));

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

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 @bobiswrong

Here is the test case that is failing.

const ledger = {
  "1": { type: "Laptop", status: "CheckedOut", borrower: { name: "Alice", email: "alice@test.com" }, dueDate: "9/5/2025" },
  "2": { type: "PC", status: "CheckedOut", borrower: { name: "Bob", email: "bob@test.com" }, dueDate: "10/1/2025" }
};
console.log(listOverdueDevices(ledger, "12/01/2025"));

Your code should output both Alice’s and Bob’s objects from the input ledger, but only the one for Alice is output.

Happy coding

Thank you for the example to help me see what’s going wrong. I’ve played with it, and I think the problem lies in the conditions for the for loops. When I adjust the first for loop condition, it fixes that one but breaks the other one. Am I on the right track, or do I need to look elsewhere for the problem?

Hi @bobiswrong

Figure out why you are swapping out the solve for each test. That will help to determine a way to handle each situation.

Happy coding

Hi, @Teller

This is what I have so far:


``` function listOverdueDevices(ledger, today) {

  let cloneLedger = loadLedger(serializeLedger(ledger));

  let todayArr = today.split("/");

  let dueDateIndexArr = \[\];

  let compareArr = \[\];

  let dueDatesStrArr = \[\];

  let sortArr = \[\];

  let overdueDevices = \[\];

//formatting todayArr

  if (todayArr\[0\].length === 1) {

    todayArr\[0\] = \`0${todayArr\[0\]}\`;

  }

  if (todayArr\[1\].length === 1) {

    todayArr\[1\] = \`0${todayArr\[1\]}\`;

  }

  let todayStr = todayArr\[2\] + todayArr\[0\] + todayArr\[1\];

  console.log(todayStr);


//puts due dates into an array


  for (let i = 1; i < Object.keys(cloneLedger).length + 1; i++) {

    if (cloneLedger\[i\].status !== "CheckedOut") {

      continue;

    } else {

      dueDateIndexArr.push(cloneLedger\[i\]);

    }

  }


//splits individual due dates

  for (let date of dueDateIndexArr) {

    let dateArr = date.dueDate.split("/");

//formats due dates

    if (dateArr\[0\].length === 1) {

      dateArr\[0\] = \`0${dateArr\[0\]}\`;

    }

    if (dateArr\[1\].length === 1) {

      dateArr\[1\] = \`0${dateArr\[1\]}\`;

    }

    compareArr.push(dateArr);

    let dueDateStr = dateArr\[2\] + dateArr\[0\] + dateArr\[1\];


    dueDatesStrArr.push(dueDateStr);

  }

  console.log(compareArr);

  console.log(dueDatesStrArr);


  //compare dates in array with today's date

  for (let i = 0; i < dueDatesStrArr.length; i++) {

    if (dueDatesStrArr\[i\] < todayStr) {

      sortArr.push(compareArr\[i\]);

    }  

  }


//console.log(sortArr);

//sort objects in ascending order by date

//if (sortArr.length > 1) {

  for (let i = 0; i < sortArr.length - 1; i++) {

    if (sortArr.length === 1) {

      overdueDevices.push(dueDateIndexArr\[i\]);

      break; 

    } else if (sortArr.length > 1 && sortArr\[i\]\[2\] > sortArr\[i + 1\]\[2\]) {

      overdueDevices.push(dueDateIndexArr\[i + 1\], dueDateIndexArr\[i\]);

      break; 

    } else if (sortArr.length > 1 && sortArr\[i\]\[2\] === sortArr\[i + 1\]\[2\] && sortArr\[i\]\[0\] > sortArr\[i + 1\]\[0\]) {

      overdueDevices.push(dueDateIndexArr\[i + 1\], dueDateIndexArr\[i\]);

      break;

    } else if (sortArr.length > 1 && sortArr\[i\]\[0\] === sortArr\[i + 1\]\[0\] && sortArr\[i\]\[1\] > sortArr\[i + 1\]\[1\]) {

      overdueDevices.push(dueDateIndexArr\[i + 1\], dueDateIndexArr\[i\]);

      break;

    }

  }

  return overdueDevices;

  }

//} else {
   

 // if (overdueDevices !== \[\]) { 
 

//  }

//}

console.log(listOverdueDevices(equipmentLedger, '12/1/2025'));


const ledger = {

  "1": { type: "Laptop", status: "CheckedOut", borrower: { name: "Alice", email: "alice@test.com" }, dueDate: "9/5/2025" },

  "2": { type: "PC", status: "CheckedOut", borrower: { name: "Bob", email: "bob@test.com" }, dueDate: "10/1/2025" }

};



console.log(listOverdueDevices(ledger, "12/01/2025"));

It shows both of the date arrays now, but it won’t return the array with the objects. I appreciate any advice you can give me.

Thanks!

Hi @bobiswrong

There are two ways you can format your code to make it easier to read and test:

  1. After you copy/paste your code into the editor, select it by dragging your cursor over it then click the (</>) button in the toolbar to automatically wrap your code in backticks. (You can click on the animated demo image below to enlarge it.)

  1. Manually add three backticks on a new line above your code and on a new line after your code. Note that a backtick is NOT the same as a single quote('). To find the backtick key on your keyboard, see this post.

To see changes to your post as you make them, you can click the (M+) button on the toolbar to bring up the rich text editor:

Happy coding

Hi @bobiswrong

So your function is returning an empty array.

Write comments about what each if statement is checking, then test with input objects which will:

  • fail
  • pass

Happy coding

Hi, @Teller

Thank you so much! I finally got it to work. I appreciate your help.