Javascript to calculate times

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;
}

1 Like

I don’t see anything that calls the function. It’s just sitting there with nothing to execute it. I would probably just take all of the code out of function if you don’t need it to be inside of one.

1 Like

Somebody needs to tell ChatGPT how the Glide JavaScript column works :joy:

2 Likes

Agreed, but 90% of the code samples out there are shown with console.log so ChatGPT doesn’t know any better.

1 Like

Yes, good point.
hmm… perhaps a Community post is needed with tips for using ChatGPT generated code…

1 Like

yes, i’m not a coder. I learn with the resources that I have and the documentation for this platform is horrific to say the least :confused:

Hopefully you understood what I meant. Remove the first line with ‘function’ in it, and the last line that had the closing bracket for the function.

2 Likes

I did, thank you!

1 Like

Not sure I’d agree with that. The documentation is far from perfect, for sure. But it’s still pretty good. And the Glide University is a great learning resource :man_shrugging:

That said, the JavaScript column isn’t even mentioned in the Glide Docs at all. Whilst teaching JavaScript is way outside the scope of the Glide Documentation, the docs should at least explain how it works and perhaps give a few usage examples. I have some small influence in this area, I’ll see what I can do.

4 Likes

That’s what I meant. I always read extensively before posting and while this particular issue is obviously lack of basic coding knowledge on my part, if you put yourself in my shoes and type Java in Glide Docs, there is nothing there. The platform also doesn’t generate a error message. I usually rely on the forum posts (usually solved by you actually), but even then they are often obsolete.

A forum post with ChatGPT guidelines would be helpful considering most Glide users are beginners and ChatGPT is a great resource to get around the limitations of the platform.

2 Likes

If even the world can’t rely on Darren’s expertise, then we’re in trouble :fearful:

2 Likes