Skip to content

Commit c8d8431

Browse files
committed
Refactored code. Simplified to one return statement.
1 parent f3aa942 commit c8d8431

1 file changed

Lines changed: 14 additions & 17 deletions

File tree

‎Sprint-3/5-stretch-extend/format-time.js‎

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,30 +28,27 @@
2828
// my code:
2929
function formatAs12HourClock(time) {
3030
const hours = Number(time.slice(0, 2));
31-
const minutes = time.slice(-2)
31+
const minutes = time.slice(-2);
32+
let hours12;
33+
let timePeriod;
3234

3335
if (hours === 0) {
34-
return `12:${minutes} am`
36+
hours12 = "12";
37+
timePeriod = "am";
3538
}
3639
else if (hours === 12) {
37-
return `12:${minutes} pm`
40+
hours12 = "12";
41+
timePeriod = "pm";
3842
}
39-
else if (hours > 12) {
40-
let newhours = hours - 12
41-
if (newhours < 10) {
42-
return `${String(newhours).padStart(2, "0")}:${minutes} pm`
43-
}
44-
else {
45-
return `${hours - 12}:${minutes} pm`;
46-
}
47-
}
48-
else if (hours < 10) {
49-
return `${String(hours).padStart(2, "0")}:${minutes} am`
43+
else if (hours < 12) {
44+
timePeriod = "am";
45+
hours12 = String(hours).padStart(2, "0");
5046
}
51-
else {
52-
return `${hours}:${minutes} am`;
47+
else if (hours > 12) {
48+
hours12 = String(hours - 12).padStart(2, "0");
49+
timePeriod = "pm";
5350
}
54-
51+
return `${hours12}:${minutes} ${timePeriod}`
5552
}
5653

5754
const cases = [

0 commit comments

Comments
 (0)