Back at it again today! My plan is to continue solving these practice problems till I feel that "Oh hell yes!" sense of accomplishment. Let's get on with it.
Your mission: Write a method that will take in a number of minutes and returns a string that formats the number into "hours:minutes"
In keeping with tradition, think about how to solve this problem before doing any actual coding. I promise it's easier than Phoebe trying to teach Joey guitar cords without a guitar. #friends
The problem is asking us to convert any number of minutes given into the hours:minutes format. In order to do this there will have to be some math involved. Maybe set an hour counter equal to 0 and every time there's at least 60 that counter should go up by one. There should conversely be another counter that starts with the number passed in and decreases by 60 each time. Then it's just an issue of formatting, concatenating variables with strings and whatnot.
1. Let's go ahead and define our function with one argument. It is already provided in Ruby, but for Javascript it would be:
function time_conversion(minutes) {
}
2. I defined two new variables here:
hours, which I set equal to 0, and
min, which I set equal to the parameter,
minutes. (This last step might be repetitive, but it was how I solved the problem and it worked so I'm keeping it for now.)
function time_conversion(minutes) {
var hours = 0;
var min = minutes;
}
3. Every time
min is greater than or equal to 60, I want
hours to increase by 1 and I want
min to decrease by 60. To do this, I used a while loop. Inside the while loop, I want it to do all the math I explained above. Remember to include the counter (i -= 60) or you'll find yourself in an infinite loop.
The same three parts of a for-loop are contained in a while loop, just the formatting is different.
function time_conversion(minutes) {
var hours = 0;
var min = minutes;
var i = min;
while (i >= 60)
hours++;
min -= 60;
i -= 60;
}
}
4. Almost done! Just need to handle some special cases-- when there is less than ten minutes, you want it to read "06" or "0-whatever" not just "6". The second case you want it to handle is when there are 0 minutes, you want it to say "00". Sounds like an if statement.
function time_conversion(minutes) {
var hours = 0;
var min = minutes;
var i = min;
while (i >= 60)
hours++;
min -= 60;
i -= 60;
}
if (min === 0) {
min = "00"
return hours + ":" + min;
}
else if (min < 10) {
min = "0" + min;
return hours + ":" + min;
}
else {
return hours + ":" + min;
}
}
This problem sent me for a loop. Yes, pun intended. It literally sent me into an infinite loop multiple times. Despite having to come back to this problem on different days to give it another shot, that little victory kinda made it all worth it. It's a pretty awesome feeling of accomplishment figuring these challenges out, even if it may not be the most concise code or employ best practices.. I have to actually know how do it first, amirite? Here's to the small victories!
P.S. I move to New York in TEN DAYS. (!!!!!!!!!!!!!)
Check out past problems here: