Skip to content

Commit e1c09ba

Browse files
committed
Finished and test code. All tests pass.
1 parent 8a05fa3 commit e1c09ba

1 file changed

Lines changed: 24 additions & 3 deletions

File tree

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

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,31 @@
2828
// my code:
2929
function formatAs12HourClock(time) {
3030
const hours = Number(time.slice(0, 2));
31-
if (hours >= 12) {
32-
return `${hours - 12}:${time.slice(3)} pm`;
31+
const minutes = time.slice(-2)
32+
console.log(hours)
33+
34+
if (hours === 0) {
35+
return `12:${minutes} am`
36+
}
37+
else if (hours === 12) {
38+
return `12:${minutes} pm`
39+
}
40+
else if (hours > 12) {
41+
let newhours = hours - 12
42+
if (newhours < 10) {
43+
return `${String(newhours).padStart(2, "0")}:${minutes} pm`
44+
}
45+
else {
46+
return `${hours - 12}:${minutes} pm`;
47+
}
3348
}
34-
return `${time}:${time.slice(3)} am`;
49+
else if (hours < 10) {
50+
return `${String(hours).padStart(2, "0")}:${minutes} am`
51+
}
52+
else {
53+
return `${hours}:${minutes} am`;
54+
}
55+
3556
}
3657

3758
const cases = [

0 commit comments

Comments
 (0)