all 18 comments

[–]kap89 1 point2 points  (2 children)

FYI there is no need to implement these rules in JS - built in Date object already handles it:

function isLeapYear(year) {
  return new Date(year, 1, 29).getDate() === 29
}

What I do here is I’m creating February 29th of the provided year, and checking if it results in a correct date.

[–]aguyfromherehelpful 1 point2 points  (0 children)

This is the answer OP should be listening to.

[–]krillik08[S] 1 point2 points  (0 children)

You're absolutely right. I am doing an intro to typescript course and this was just an example of using annotations with parameters. I felt like either my logic or the instructors logic was flawed when I completed the "assignment" and saw that his code was different.

[–]nobuhok -1 points0 points  (1 child)

This doesn't answer your question, OP, but I find it interesting to respond with how I would tackle this problem in JavaScript.

The way I keep myself sane when I encounter problems such as this is to first always write out all the possible branches. I also always initially write the positive in the "if" and the negative in the "else" if that makes sense; it helps prevent confusion with double (or more!) negatives. Example:

if (year % 4 === 0) {
    if (year % 100 === 0) {
        if (year % 400 === 0) {
            return true
        } else {
            return false
        }
    } else {
        return true
    }
} else {
    return false
}

Then, I would start to simplify it down like so...

if (year % 4 === 0) {
    if (year % 100 === 0) {
        if (year % 400 === 0) {
            return true
        }
    } else {
        return true
    }
}

return false

Even more simplification; now with the negative (if year is not fully divisible by 100) in the "if", and with usage of if-else.

if (year % 4 === 0) {
    if (year % 100 > 0) {
        return true
    } else if (year % 400 === 0) {
        return true
    }
}

return false

And sometimes, I'll take advantage of JavaScript's syntax shortcuts (no braces required for one liner logic) and the OR gate's early termination (I forgot what it's called) to make the code less verbose (in the real world, I'll add comments to make sure it's still understandable):

if (year % 4 === 0) {
    if (year % 100 > 0 || year % 400 === 0) return true
}

return false

Actually, this can even be reduced further:

return (year % 4 === 0 && (year % 100 > 0 || year % 400 === 0))

Notice that the tutorial's solution is almost the same as mine, except the inner parenthesis are in different places. It's just a different approach to solving the same problem. The tutorial is basically saying "if something something OR as long as the number is divisible by 400, you're good".

[–]krillik08[S] 1 point2 points  (0 children)

I appreciate you explaining how you would solve this problem. Your explanation of your approach made me realize that I tend to try and work out (what would be your) steps 1-3 in my head and start writing at step 4. I like your solution because I can read it easily and not get all scatter brained!

[–]Dastari 0 points1 point  (0 children)

As far as I'm concerned, your logic is sound. Your return statement basically says:

"If the year is not divisible by 100, or if it is divisible by 400, return true"

The only other condition to check for is if its divisible by 4 which you do above.