Skip to content

18m Solution #114

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 18m
Choose a base branch
from
Open

18m Solution #114

wants to merge 1 commit into from

Conversation

SuperSimpleDev
Copy link
Owner

No description provided.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have noticed that the estimatedDeliveryTime response from the POST /orders does not use isWeekend().

Screenshot from 2025-03-12 14-11-40

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it does not. But the "skipping weekends part" was an exercise. Lesson 15 Exercise 15m.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@a10n-jsd use this code

const matchedOption = findDeliveryOption(order.orderTime, productDetails.estimatedDeliveryTime);
console.log(matchedOption);

let deliveryDate = 'undefined';
if(matchedOption){
       deliveryDate = calculateDeliveryDateFrom(order.orderTime, matchedOption);
}
 <div class="product-quantity">
     Quantity: ${productDetails.quantity}
 </div>
// all this code for skipping weekends from product-delivery-date
// to skip weekends we need deliveryOptions.deliveryDays 
// but the products we get from backend doesnt have deliveryOption, and we cant change backend
// so here is the code for finding an deliveryOption, which we can use in our function

function countDaysBetween(startDate, endDate) {
    // .diff is built-in method of dayjs which calculates tha difference between two dates in days
    return dayjs(endDate).diff(dayjs(startDate), 'day');
}

function findDeliveryOption(orderTime, estimatedDeliveryTime){
    const daysBetween = countDaysBetween(orderTime, estimatedDeliveryTime);
    return deliveryOptions.find(option => option.deliveryDays === daysBetween) || null;
}

Alternate version of calculateDeliveryDate(). Put this code in deliveryOptions.js

export function calculateDeliveryDateFrom(orderTime, deliveryOption){
    let orderDate = dayjs(orderTime);
    let daysAdded = 0;

    while(daysAdded < deliveryOption.deliveryDays){
        orderDate = orderDate.add(1, 'day');

        //skip weekends: 0 = Sunday, 6 = Saturday
        if(orderDate.day() !== 0 && orderDate.day() !== 6){
            daysAdded++;
        }
    }

    return orderDate.format('MMMM D');
}

this calculates not from current date but the day we ordered

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants