-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
143 lines (116 loc) · 3.18 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
# What time is it? Old Solution
## Description
Help us to translate the given time into a sentence that states that time it self
## Hint
- time : String time that must be translated into a sentence
- if time = 12:00 am return must be "It's midnight"
- if time = 12:00 pm return must be "It's noon"
- else return must be what time is it and it's afternoon or morning
## Examples
> - time = 2:15 pm
> - return = It's two past fifteen in the afternoon
> - time = 8:57
> - return = It's eight past fifty seven in the morning
---
*/
function whatTimeIsIt(time) {
var ones = [
"",
"one",
"two",
"three",
"four",
"five",
"six",
"seven",
"eight",
"nine",
"ten",
"eleven",
"twelve",
"thirteen",
"fourteen",
"fifteen",
"sixteen",
"seventeen",
"eighteen",
"nineteen",
];
var tens = [
"",
"",
"twenty",
"thirty",
"forty",
"fifty",
"sixty",
"seventy",
"eighty",
"ninety",
];
var numString = num.toString();
if (num < 0) throw new Error("Negative numbers are not supported.");
if (num === 0) return "zero";
//the case of 1 - 20
if (num < 20) {
return ones[num];
}
if (numString.length === 2) {
return tens[numString[0]] + " " + ones[numString[1]];
}
//100 and more
if (numString.length == 3) {
if (numString[1] === "0" && numString[2] === "0")
return ones[numString[0]] + " hundred";
else
return (
ones[numString[0]] +
" hundred and " +
convert(+(numString[1] + numString[2]))
);
}
if (numString.length === 4) {
var end = +(numString[1] + numString[2] + numString[3]);
if (end === 0) return ones[numString[0]] + " thousand";
if (end < 100) return ones[numString[0]] + " thousand and " + convert(end);
return ones[numString[0]] + " thousand " + convert(end);
}
}
function getTimeZoneWording(timeZone, hour) {
let wording;
switch (timeZone) {
case "am":
if (hour == 12) {
wording = "midnight";
} else {
wording = "morning";
}
break;
case "pm":
if (hour == 12) {
wording = "noon";
} else {
wording = "afternoon";
}
break;
default:
break;
}
return wording;
}
function solution(times) {
let splittedTimes = times.split(" ");
let timeZone = splittedTimes[1];
let hourAndMinute = splittedTimes[0];
let splittedHourAndMinute = hourAndMinute.split(":");
let hour = splittedHourAndMinute[0];
let minute = splittedHourAndMinute[1];
let timeZoneWording = getTimeZoneWording(timeZone, hour);
let hourWording = humanize(hour);
let minuteWording = humanize(minute);
return `It's ${hourWording} past ${minuteWording} in the ${timeZoneWording}`;
}
console.log(solution("2:15 pm"));
console.log(solution("8:57am"));
module.exports = whatTimeIsIt;