This Talking Clock challenge was the first and easiest challenge I completed at Grand Rapids CC. In fact, I think everyone in the group at least finished this challenge, but seeing all the unique approaches was pretty neat. We all gave a presentation on how we did things, and I walked away wishing I had done a few things differently I think. Taken step by step it's both an interesting and fairly straight forward problem.
The first step of solving any problem is to get your workspace ready, and programming is no different. That includes having all the variables necessary to spell out the time in both hours, minutes, and combine them. As well as having the means to capture and store user input.
However, once we get the user's input we need to process it in such a way that we can compare values. First I turn the two digits of the hour into an int from a string, and then I perform a comparison to find the appropriate string for the hour. If I had put more thought into the code here, and the same for seconds it could have worked with a single digit instead of double digits. Checking the length of substrings based on position relative to the colon would have made that possible. I suppose this approach was a bit more straightforward.
I'm about to write nearly identical code for the minutes portion. I wish I had just turned this into a function instead. It just meant that some of the values would have been ignored if I fed it the hours number, and that would have been better than writing the same code twice. In fact, the way the string is processed, converted, and thrown through an if-statement is the same.
The final bit just creates an output for the user. It combines the strings of the hour and minute and either "am" or "pm" depending on which is appropriate.
All-in-all a pretty neat but straight forward challenge. There were things I could have improved looking back at things. Particularly, there is a ton of redundancy that could be removed with the use of a few strategic functions. Either way, I recall it taking nowhere near as long as I expected. If you want to see the source code on Github it is available here. Otherwise, there is a video demonstration below.