getify / You-Dont-Know-JS

A book series on JavaScript. @YDKJS on twitter.
Other
177.86k stars 33.39k forks source link

YDKJSY, coercion practice question #1861

Closed ggepenyan closed 3 months ago

ggepenyan commented 3 months ago

I already searched for this issue

Edition: 2nd

Book Title: YDKJSY

Chapter: Appendix B

Section Title: Practicing Comparisons

Question: isn't this solution correct/valid? Why the solution needs to be that long and hard to understand?

function scheduleMeeting(startTime, durationMinutes) {
    const tmpStartTime = Number(startTime.replace(':', '.'));
    const tmpDayStart = dayStart.replace(':', '.');
    const endTime = tmpStartTime + Number('0.' + durationMinutes);
    const tmpDayEnd = dayEnd.replace(':', '.');

    if (tmpStartTime < tmpDayStart
        || endTime > tmpDayEnd
    ) {
        return false;
    }
    return true;
}
habvh commented 3 months ago

@ggepenyan I think the author wants to divide it into small codes that are helpful for readers logically and use a coding pattern for example. Sometimes, it will be easier to understand than trying to write a shortcode. But that all is my own opinion.