Created
May 18, 2018 03:52
-
-
Save MorenoMdz/22009477e4e371744cdfa0ed0bcad79e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| This challenge requires you to convert an integer, which represents the number of minutes, for example 63 means 214 minutes, and convert this integer into hours and minutes. So if the input was 63, then your program should output the string '1:3' because 63 minutes converts to 1 hour and 3 minutes. | |
| We will use the modulo operation to solve this challenge. The modulo operation simply returns the remainder after a division, so for example, the remainder of 5 / 2 is 1, so the modulo of 5 / 2 is 1. | |
| */ | |
| function TimeConvert(num) { | |
| // to get the hours, we divide num by 60 and round it down | |
| // e.g. 61 / 60 = 1 hour | |
| // e.g. 43 / 60 = 0 hours | |
| var hours = Math.floor(num / 60); | |
| // get the remainer from the the floor math and module by 60; | |
| var minutes = num % 60; | |
| // Build the new format | |
| return `${hours}:${minutes}`; | |
| } | |
| /* Simple return */ | |
| function TimeConvert(num) { | |
| // code goes here | |
| return Math.floor(num / 60) + ":" + num % 60; | |
| } | |
| /* Declarative */ | |
| function TimeConvert(num) { | |
| let hours = 0; | |
| let minutes = 0; | |
| minutes = num % 60; | |
| hours = (num - minutes) / 60; | |
| return hours + ":" + minutes; | |
| } | |
| /* Appending +1 hour (60min) in each loop iteration */ | |
| function TimeConvert(num) { | |
| var hours = 0; | |
| var mins = num % 60; | |
| // code goes here | |
| for (var i = 60; i <= num; i += 60) { | |
| hours++; | |
| } | |
| return hours + ":" + mins; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment