Here's one more task, which requires a little bit of thinking.
And as you can see, we must try to function, which can convert the time input given in 12 hours format
to 24 hours format.
And here we have use cases.
This is why it is easier for us to check if we covered everything correctly.
So let's try to create this function.
And once again, this is not a function that you can solve in 30 seconds.
So you can take your time and think about it a little bit.
So let's create here, convert to 24 hour format, and we're getting hit time text.
And actually what they want to do here first, I want to convert it to lowercase because actually here
we will get AM and PM, and we want to be sure that they are lowercase.
This is why here we can try time text lower.
And here we want to call time text to lowercase.
The second thing that I want to do is get hours and minutes separately.
So what I want to write here is time text lower split and we want to split it by column because actually
we have Colin in every single case and on the left we have ours and on the right we have minutes.
And actually, here, I want to make it a structured an assignment here directly.
I want to get first of all hours and secondly minutes.
In this case, we're creating two local variables.
If you don't want to write like this, you can name it with some temporary variable and then just read
this temporary variable that hours and minutes.
But here we come to difficult stuff.
As you can see, sometimes we have just a single digit and sometimes we have two digits, but also we
have a case where we have zero at the beginning.
And here we also have one more case that we need to cover if we have here 12 hours.
Then we must convert it to zero zero.
This is why I want to write exactly this logic.
So if time ticks lower dot and with and actually end with is a really nice function in JavaScript,
which has shaken the toast string you send in with something.
And here we can check for AM because actually we know that here we have 12 a.m. and it means that we
must convert it to two zeros.
This is why here I want to write hours equals.
And here we are checking if our hours equals 12 and 12 as Austrian, because here we are, split in
Austria and then we want here to get zero.
In neither case, where get in here hours?
And as you can see, first of all, I wrote here a single zero and not two zeros.
And this is also important.
And here I just directly assigned ours, which actually means we covered this logic to get a single
zero.
And in all other cases, we just get whatever we get.
If it is five baguette and five if it is zero one, we will get zero one.
So recovered our AM case.
Now I want to cover PM Case.
This is way here we can write owls and even may be elusive because we want to check for PM.
And here we can write time, text, lower dot and ways.
And here we get getting our PM.
And in this case, we want to apply different logic.
We want to sign here hours and we want to check if our hours equals 12th because this is again another
case.
In this case, we want to apply here hours or we want to write history in and inside plus hours plus
12.
But this code is doing a toll, as you can see here, I wrote plus hours what it does.
Actually, we're getting our hours as a stream.
So let's say that we're getting here 12 now, if I will, right here.
Plus, at the beginning, we're getting here directly.
No from a stream, which actually means we're converging this two number.
And then we make +12.
Why is that?
Because let's say here where checking PM, for example, this case with zero one.
And here we are.
Chicken, OK, hours equals 12 now and then we want to make zero one plus 12.
Let's check the South how it will be.
So we're getting here zero one.
And actually it is converted to one.
And now after this, we're getting +12 and we're getting to 10, which actually means by adding 12,
we're converting it to the correct PM time.
And if we have 12 pm like this, then we simply leave it like 12.
So actually here we could write 12 over could just take our hours.
So what I want to do now, I want to check what we're getting inside hours.
So here I want to console.log our convert to 24 hour format.
And here is our time text and our hours.
And now I want to copy all these cases and just run them.
In this case, we can check that they're working correctly.
I'm not allowed in the page and where I get an assignment to constant variable.
And as you can see here, we're trying to override ours.
And of course, it's forbidden.
This is where whoever must write, Let, let's check this out.
I am delighted and we are getting 412 zero four five five 12 12 zero one 13.
Here we have PM 11 to three and 10 22, which actually means our logic for hours is correct.
After this, we must add a leading zero in this whole cases.
And as you can see, sometimes we don't need it and sometimes we must edit and we can easily solve it
with one more plain JavaScript function.
We can write here hours dot pads tired.
And here we can provide to zero.
And what it does is you can see here it pads the current screen with another screen multiple times,
if needed.
Which actually means here we're providing tool, which means we will have two symbols and here we have
zero, which actually means if we don't have a symbol there, then we will put zero inside.
And actually now I want to copy this part and just put inside our console.log to check if it's working.
I am reloading the page and as you can see now, we're getting the correct time.
So here we have two zero zero five and then all our correct numbers.
Now we must implement our minutes and actually it is much easier.
First of all, just to remind you, inside minutes, we'll also have P, M and M, and we must get rid
of it.
This is where we can write here directly means dots lies.
And here we are using zero minus two.
In this case, we will get rid of two symbols AM or PM and we will get just minutes here.
And after this, I want to use pets tired just like we did previously, and here will be also two and
zero.
In this case, we will check, okay, if we have here eight, then we must get here zero eight and if
we will have here two digits, then it is totally fine.
Now we can just remove this console.log, but don't need it anymore.
And actually, here in every single case, I want to write console.log.
This is why let's change this now.
I want to write here console.log, then open it.
And here we have convert.
And let's close.
It was break it.
Now we have all our cases.
Let's check them out, aim to load in the page and we're getting our time and it is looking totally
fine actually.
Here, for example, we have minute zero zero three three five two five zero eight and so on.
But the problem is that we forgot to put a colon between them.
This is where human must write plus and between them just put a strain with the colon.
And as you can see now, we get a nice time in 24 hours format.
And actually, from my point of view, this is difficult to solve this task in five or ten minutes.
You really need to sit there like maybe 15 minutes or half of an hour to check different cases, how
you want to solve it and so on.
So this is a nice task to check how you can convert different things between them with different logic,
but it is not something that you can solve in one or two minutes.