Alright, so let's try to tackle this challenge step by step. Now, if we look at the flowchart, the first
question we ask is, is it cleanly divisible by 4?
If no, then it's definitely not a leap year.
If yes, then we have to ask more questions.
So how would we write this in terms of code?
Well, we'd probably use an if statement to check if the year that were being passed in here is cleanly
divisible by 4.
And remember that on a previous lesson, which I linked to here, we learned about the modulo operator
which gives you the remainder of a division.
So if the year % 4 === 0, namely if there is no remainder when you divide the
year by 4, well in that case then it's likely to be a leap year.
But if it's not even divisible by 4, then it's definitely not a leap year.
So here we could simply return "Not leap year."
So any year that is not divisible by 4, then it's definitely not a leap year.
But if it is divisible by 4, we still have to check
is it cleanly divisible by 100?
Because if no, then it's definitely a leap year,
but if yes, then we have to check further.
So the best way to represent that logic is to create another if statement inside this if statement.
So it's already divisible by 4
but we have to check whether if it's divisible by 100.
So if the year % 100 is equal to zero, well, then we have to make some further checks.
But if it's not, then it's definitely a leap year. So we can return leap year.
Now, finally, we're in the homestretch and we have to ask ourselves the last question.
Is it cleanly divisible by 400?
If no, then it's not a leap year,
if yes, then it is a leap year.
So inside this if statement, we're going to ask ourselves one further question:
if the year is divisible by 400 and that has no remainder, well, then it's definitely a leap year.
And if that's not true, then it's definitely not a leap year.
So this is the logic that we saw in the flowchart mapped out with JavaScript code. And going from these
words to code is actually quite a big jump.
But going via that middle ground, that flowchart, makes it so much easier to actually map out the logic
and then converting it into code.
So I hope you enjoyed that challenge and you managed to get it right.
And at least if you didn't, you saw how you can use flowcharts to simplify and clarify your own idea
of the logic, because at the end of the day, the code is easy, but the logic is the part that you
got to get right first.
So now if we check our solution, then everything should pass.