Hey FreeCodeCamp welcome to another the daily programmer web series video the problem today
We're going to talk about is called talking clock
Which is number three to one let's go ahead and look at the subreddit and read through it alright
so for this problem the description says no more hiding from your alarm clock you decide you want your computer to keep you updated from
Time to time, so you're never late again a talking clock takes a 24 hour time and translates it into words
so for the input, we're given
This type of string so military time for instance
We have the string 20 : 29 which would be 8 29 and then our goal is to take that input string in to print
Out its string representations. So in this case 2029 should print out its 829 p.m.
And then for 00 if you print out its 12 a.m.
So from reading the description just looking at the input data in the output data
It should be pretty straightforward as to what we're trying to do
So let's start breaking this down in tempo mania on the white board alright, so let's look at a single example here
Where we have 22?
: 35 and the first thing that comes to mind is when you have something like this you need to kind of process it from
You know the hours and the minutes
It's useful to kind of extract your hours separate from your minutes
So you can go ahead and split this string on the colon
which will give you an array of
22 and 35 as string form and then you can
Convert those to integers using the Partizan method or you know math the array and pass it apart and function
But regardless when you end up with 22 and 35 we're going to be using these to kind of generate these three subproblems
So if you were to look at the main problem that we're trying to solve there are three different
Things you kind of have to do and you could do this separately so the first one is
How do you print out the hour? So that's like?
How you print out one o'clock two o'clock three o'clock to 12 o'clock. How do you print out?
The minute is so problem 2 which will be the hardest and we'll save that one for last
And that would be like how do you print out?
If it's zero five zero five in this case thirty five, how do you print out 35 am or pM and?
Then the third sub problem is how do you print out am or pM?
and
Again since this military time zero is 12 a.m.. 12 is 12 noon which is 12 p.m.
and then 23 would be a
11 p.m
So out of these three sub-problem. Let's go ahead and start with the easiest one which would be print a.m.
P.m.. So to do that this one should be pretty straightforward, so if you were to take the our
representation here so 22
How would you check if this is in the afternoon or if this is in the morning like a.m.
Well, basically if it's less than 12
We know that should be a.m.
Otherwise it should be p.m.
So in this case
22 would be 10 o'clock at night so we can just use this if statements to determine if it's amber p.m.
And that's pretty straightforward so in this case. We're going to say it's p.m.. Go ahead and write p.m.
At the end of your string and see that for later
So we solved the first sub problem. Which is hedy to determine if it's a more p.m.. Pretty straightforward and
Then let's move on to sub problem 1. It's how do you print out the hour?
So to do this first we want to declare some type of structure
so you can divert the number that we have as the input and
Determine that the string. It needs to be out put it right so over here
I went ahead and defined an array which represents the different hours
We can print out so 12 1 2 3 4 all the way to 11 o'clock
And I'm mapping some index to those so it turns out that you know these are integers
And they map pretty well to the indexes of arrays
so I went ahead and just decided to choose an array to kind of figure out the conversion, so
To map let's say in number 22 to the corresponding number. It needs to be over in this hours array
We could simply just mod it by 12. Which is the number of hours that you have in each segment of the day a.m.
And p.m.. So if you do 22 Mod 12
we're left with 10 and
If you use that index 10 and figure out what it needs to be in this hours array?
Index 10 should point to the string 10
So in this case down here. We could just say you know print out
hours of
22 Mod 12 and that should give us index 10 which would then print out the string?
Ten
I'll just go ahead and start this with it's
Right and so if you were to do something else such as instead of let's say this was in tWenty-two
And I said this is twelve
We'd be left with the index of zero
which would give us 12 and
If the input was let's say zero
We're also going to get zero which is twelve because you remember zero zero is 12:00 a.m.
Which again will work as we intended to so that's how you do the subproblem print hours pretty straightforward as well
just declare an array or each index of the array points to the string representation of that index and
Then just mod your input hours to by 12 to get whatever index you need to print out
So let's move on to the last part of this problem
Which is the harder part which is printing out the ministering?
So to start off there's there's a couple of different subproblems the first step problem for printing the minute is
Let's say it's a number that's less than 10 right those are single-digit numbers, so in this case
let's say this is o 5 instead of 35 we need to print out o
5
So again, we can still do like the same logic here
but instead of using an hours array, we can go ahead and just use a
Ones Array and the ones array same idea of the hours array, but instead of 0 through 11
We have indexes 0 through 9 where 0 is a blank string
1 is the string 1 ETC until 9 we have the string 9 so in this particular case?
How do you print out if let's say we had a 5 as our input?
How would you print out a 5 here right? So if it is less than 10?
we know that we need to use this ones array to kind of figure out what the string needs to be and
again, we could just
Take the digit as our index and print it out here
So in this case we get oh
5 here which would be cast to 5 if we parse it as an int and then our index 5 would just print out 5
And we can prefix with oh
Then concatenate it with whatever that is again. I'll write out
the first case which is if
m is Less than 10
And again the first case and most of ten we just need to print out single-digit the second case
if
m is less than twenty
We need to print out the teens right so 13 14 15 11 12, whatever so let's go ahead and declare another
array called teens
Which we can use to again map numbers from 10 to 19
Using the same process so again this one once we get m
We're actually Gonna have to mod it by 10
So we can get what value it is in this array. So in this case if m is let's say
15
If we mod that by chin, we'll get 5 and if we look up index 5 and this tins array or teens array
5 should equal the string 5
if we try it with let's say 10 mod 10
We're going to be left with 0 which will give us
10 is our string if
we Mod it by
19 by 10 we're going to be left with Index 9
Which again over here 9 is 19?
So we saw sub-problem if m is less than 10
We set the subproblem if n is less than 20 now for the last sub problem is else
So otherwise it's greater than or equal to 20
Now in this case. We need yet another array, which will be called our kins array
And what this array is used for is printing out the tens digit
So in this case 35 we need to print out 30 and then we also need to print out 5
So taking that example. Let's just go ahead and say okay
well How do we print out 30 so we have this tens array over here index is 0 through 5?
We need to get index 3 right the digit 3 will represent 30 down here
So to do that. We can just do m divided by 10 and then floor that
And that should leave us with 3 in this case in next 3 will give a sturdy
go ahead right, it's 10:30 and
then the last part of this
Is we need to get the ones digit so I can say em?
mod 10 in this case 35 mod 10 will give us 5 and
If we were looked at up in the ones array over here
we should get the string 5 so if I were to just do that, so we can pronounce 5 here and
That's basically how you work out this problem in
This sub problem where if you were to print out 35, we just use the ones array we use a tens array
You kind of connect concatenate them together we can mod by 10 or divide by 10 to get the different digits?
So divide by 10 gives us 3 mod by 10 gives us 5 look those up and then print to my down here
So finally the single input 22 : 35 will give us the output it's 10:35 p.m.
hopefully this is a good overview of how to break this down into different subproblems and
Again how to break in break down this sub problem 2 which is print minute into the different cases that we need to handle
There's probably a different way to do this
But I think this is the easiest that comes to my mind all right so again let's implement this in Javascript
Alright so start off with the implementation
I went ahead and declared those 4 arrays that are used for mapping the integer value to the string representation. So you see here
We have a capital ours array. Which has all the 12 to 11. We have the capital one's array
Which has all the different ones digit so 1 2 3 4 5 7 8 9 we have two teams array
Which is used for mapping
all those different cases where we have 10 through 19 and
Then finally we have the tins array
Which is used for mapping the tens digit of cases that are greater than equal to 20 and then down here?
which is go ahead and start with the use case of
0 0 comma 0 0 and let's start declaring the function called talk which takes 8 time as an input parameter
So the first thing we want to do again is take that time string and we go ahead and split it
pars the int and map it to an array like say const of
H m is equal to time we've split and then split it by the colons and then map it to by the pars int
function
And then I'll go ahead and return the empty string here just for now
So just to kind of see what's going on here. I'll print out hMM
Those both print out zero so that's expected
if I were to change this to let's say 1 & 1
Those both print out 1 & 1 so that's working as intended
So let's first start with the first sub problem
Which is how do you print out the our string so to do that remember? We have the hours array? Which we can grab?
H from and then begin to Mod by 12 and
then we could say const hour is equal to that and
If I were to print out our down here we see that it prints out 12
So we just solved the first sub problem, which how do we print out the hours and you see down here?
We're printing out 12 which is expected and then before we take a step forward
I'm just going to go ahead and just print out or start working on an array where we can say it's and
Then also pre mount hours, and I'm going to join those
with an empty string
such is kind of building up for a final string that we're supposed to print hasn't our output and
Then the second sub problem is how do you print out the suffix of am and pm so again?
We can say const suffix is equal to and then we use a ternary operator here say h less than 12
Then we need to print out a.m.
Otherwise we print out p.m.. And we can just go ahead and put suffix into that final array that we're returning
So you see down here at printing out it's 12 a.m.
If we change this to 12 it prints out its 12 p.m.. So this is all working as intended. It's 11 p.m.. Works fine
and
Now for the third sub problem, which is where we have to have those different cases. Let's just go ahead and declare a minute
object and
Then I'm going to go ahead and put that here so we can print out whatever the minute value is
In the first case is if m is less than 10 remember we need to print out the ones arrays
Corresponding mapping value, right so we can say minutes is equal to
one of Em
And I'll go ahead and change this to
2301 It says it's 11:00 1:00 p.m.. So I forgot to put the o here
So now it's printing out it's 1101 p.m.. And then go ahead and test out your edge cases of 0-9 it's 11 O 9:00 p.m.
So that's working as intended by change this the tin
Probably won't work as we went to because it should print out its
11:10 p.m.. And we haven't implemented that case yet? So let's move on to the second case which is if
m is Less than 20
we need to set minute equal to and then we could say the
teens and
Then m divided by 10 like we did on the example
Go ahead and close off that curly brace
So now in this case that's working fine, so it's 11:10 p.m.
If I change this to 19 it should pronounced 1119 p.m.. So again
We just implement a case 1 which is it is less than 10?
Case 2 for the teens if it's less than 20
And then finally we use the or finally we write the last case which will be if it's greater than equal to 20
So we could set minute equal to and then first we need to do the tens
again we floor in divided by 10 here, and that will give us our tins string representation of the tens digit and
Then followed by space we can do
grab the ones array
pass it in Mod 10 and
That will give us the one string are the ones digit string representation
And then we can test that out here if you put 23 20, it says it's 11:20 p.m.. So that's good
It's 11:29 p.m.. When we change this to 29
Change it to 59 its 11:59 p.m.. So basically we've implemented the same solution that we worked on the whiteboard and
kind of as a recap we declared those four arrays and
we're using those arrays to kind of determine what the string value should be based on the integer values and we use the
index for that mapping
alright, so that wraps up the implementation of the talking clock problem again if you liked this tutorial be sure to like the video and
Subscribe to free code Camp below all right. Thanks for watching