Tell us what’s happening:
Hi Everyone,
For some reason my code fails checks 18-20 (listOverdueDevice) though I’m pretty sure it returns correct devices in correct order. Any hint on what I’m missing?
Your code so far
const equipmentLedger = {
"1": { type: "PC", status: "CheckedOut", borrower: { name: "John Smith", email: "john@acme.org" }, dueDate: "1/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/1/2025" },
"4": { type: "iPad", status: "CheckedIn", borrower: { name: "", email: "" }, dueDate: "" }
};
const checkinDevice = (ledger, assetTag) => {
let confirmationString = `Asset ${assetTag} checked in`;
let counter = 0;
let updatedLedger = structuredClone(ledger);
if (updatedLedger[assetTag])
{
updatedLedger[assetTag].borrower.email = "";
updatedLedger[assetTag].borrower.name = "";
updatedLedger[assetTag].dueDate = "";
updatedLedger[assetTag].status = "CheckedIn";
counter = 1
}
if (counter === 0) {confirmationString = `Asset ${assetTag} not found`}
return { ledger: updatedLedger, message: confirmationString}
}
const checkoutDevice = (ledger, assetTag, borrower) => {
let confirmationString = `Asset ${assetTag} checked out to ${borrower.name}`;
let counter = 0;
let updatedLedger = structuredClone(ledger);
if (updatedLedger[assetTag]) {
if (updatedLedger[assetTag].status === "CheckedOut") {confirmationString = `Asset ${assetTag} already checked out`}
else {
updatedLedger[assetTag].borrower = borrower;
updatedLedger[assetTag].status = "CheckedOut";
counter = 1
}
}
if (counter === 0) {confirmationString = `Asset ${assetTag} not found`}
return { ledger: updatedLedger, message: confirmationString}
}
const listOverdueDevices = (ledger, today) => {
let overdueDevices = [];
let todayArr = today.split("/");
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];
let updatedLedger = structuredClone(ledger);
//console.log("Today is " + todayStr);
for (let i = 1; i <= Object.keys(updatedLedger).length; i++) {
//console.log(updatedLedger[i])
if (updatedLedger[i].status === "CheckedOut") {
let dateArr = updatedLedger[i].dueDate.split("/");
if (dateArr[0].length === 1) {dateArr[0] = "0" + dateArr[0]}
if (dateArr[1].length === 1) {dateArr[1] = "0" + dateArr[1]}
let dateStr = dateArr[2] + dateArr[0] + dateArr[1];
//console.log(dateStr);
if (parseInt(dateStr) > parseInt(todayStr)) {
updatedLedger[i].dateStr = dateStr;
overdueDevices.push(updatedLedger[i])
}
}
}
overdueDevices.sort(function(a, b) {return parseInt(a.dateStr) - parseInt(b.dateStr)});
for (let device of overdueDevices) {delete(device.dateStr)}
return overdueDevices
}
const loadLedger = (str) => {return JSON.parse(str)}
const serializeLedger = (ledger) => {return JSON.stringify(ledger)}
console.log(listOverdueDevices(equipmentLedger, "9/11/2001"))
Your browser information:
User Agent is: Mozilla/5.0 (X11; Linux x86_64; rv:140.0) Gecko/20100101 Firefox/140.0
Challenge Information:
Build a Device Loan Ledger - Build a Device Loan Ledger