Skip to content

Commit

Permalink
Time to English
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinsosborne committed Oct 23, 2017
1 parent a6ac280 commit af34b6f
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions Chapter 7 - Arrays, Part II/Time to English.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// You are given an integer representing the number of minutes that have elapsed since midnight. You should return a string representing the current time, in traditional spoken convention. Use numerals, except specifically the following words - midnight, noon, past, til, half, quarter. Examples: if given 30, return "half past midnight". If given 710, return "10 til noon". If given 1000, return "20 til 5 pm".

function timeToEnglish(num){
if(num % 60 == 0 && (num / 60) < 12) {
var currentHour = num / 60;
return "It is " + currentHour + " am";
}
else if(num % 60 == 0 && (num / 60) > 12) {
var currentHour = num / 60;
var currentHour = currentHour % 12;
return "It is " + currentHour + " pm";
}
else if(num % 60 == 30 && (num / 60) < 12){
var currentHour = num / 60;
if(Math.floor(currentHour) == 0){
return "It is half past midnight";
}
else{
return "It is half past " + Math.floor(currentHour) + " am";
}
}
else if(num % 60 < 30 && num % 60 != 25 && (num / 60) < 12) {
var currentHour = num / 60;
var currentMinute = num % 60;
if(Math.floor(currentHour) == 0){
return "It is " + num + " past midnight";
}
else{
return "It is " + currentMinute + " past " + Math.floor(currentHour) + " am";
}

}
else if(num % 60 == 25 && (num / 60) < 12) {
var currentHour = num / 60;
var currentMinute = num % 60;
if(Math.floor(currentHour) == 0){
return "It is a quarter past midnight";
}
else{
return "It is a quarter past " + Math.floor(currentHour) + " am";
}

}
else if(num % 60 == 45 && (num / 60) < 12) {
var currentHour = num / 60;
var currentMinute = num % 60;
return "It is a quarter til " + Math.ceil(currentHour) + " am"
}
else if(num % 60 != 45 && num % 60 > 30 && (num / 60) < 12) {
var currentHour = num / 60;
var currentMinute = num % 60;
var remainingMinute = 60 - currentMinute;
return "It is " + remainingMinute + " til " + Math.ceil(currentHour) + " am"
}
}


console.log(timeToEnglish(105));

0 comments on commit af34b6f

Please sign in to comment.