I’m creating a javascript function for a project to calculate estimated delivery dates of orders based on # of pages, and whether it’s expedited or not. I also need the function to move any delivery dates that fall on Sunday to Monday at 10AM. I have the code but it’s not working and system doesn’t give me an error code. Does anyone have any suggestions?
In the code P1 is the date the order was created. I’ve tried the date in ISO format and now MMM dd, yyyy format but nothing. P2 is # of Pages and P3 is whether it’s expedited “Yes”, regular processing “No”, or 2 hour delivery.
CODE:
function calculateDeliveryTime(p1, p2, p3) {
// Parse orderDate and numOfPages
const [month, day, year] = p1.split(’ '); // Assuming input like “Dec 22 2023”
const orderDate = new Date(${month} ${day}, ${year}
);
const numOfPages = Math.ceil(p2); // Rounding up decimal values
// Define fulfillment times in hours for different page ranges
const fulfillmentTimes = {
‘No’: { ‘1.0-3.0’: 24, ‘4.0-8.0’: 48, ‘9.0-15.0’: 72, ‘16.0-20.0’: 96 },
‘Yes’: { ‘1.0-3.0’: 12, ‘4.0-8.0’: 24, ‘9.0-15.0’: 48, ‘16.0-20.0’: 72 },
‘2 Hour Delivery (1-3 Pages Only)’: { ‘1.0-3.0’: 2 }
};
// Function to get the fulfillment time for a given page range
const getFulfillmentTime = () => {
if (numOfPages <= 3) return fulfillmentTimes[p3][‘1.0-3.0’];
if (numOfPages < 8) return fulfillmentTimes[p3][‘4.0-8.0’];
if (numOfPages <= 15) return fulfillmentTimes[p3][‘9.0-15.0’];
if (numOfPages <= 20) return fulfillmentTimes[p3][‘16.0-20.0’];
if (numOfPages <= 30) return fulfillmentTimes[p3][‘21.0-30.0’];
return null;
};
let fulfillmentTime = getFulfillmentTime();
if (fulfillmentTime === null) return ‘TBD’;
// Calculate delivery date
let deliveryDate = new Date(orderDate);
deliveryDate.setHours(deliveryDate.getHours() + fulfillmentTime);
// Adjust for Sundays
if (deliveryDate.getDay() === 0) {
deliveryDate.setDate(deliveryDate.getDate() + 1); // Move to Monday
deliveryDate.setHours(10, 0, 0, 0); // Set time to 10:00 AM
}
// Format the date in Month Day Year format
const formattedDeliveryDate = deliveryDate.toLocaleDateString(‘en-US’, { month: ‘short’, day: ‘numeric’, year: ‘numeric’ });
return formattedDeliveryDate;
}