Skip to content

Commit 2fd2099

Browse files
committed
stretch
1 parent fcd1dc1 commit 2fd2099

2 files changed

Lines changed: 26 additions & 33 deletions

File tree

Sprint-2/1-key-errors/2.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55

66
// =============> write your prediction of the error here
77
//we can't declare number as a variable
8-
function square(3) {
9-
return num * num;
10-
}
8+
// function square(3) {
9+
// return num * num;
10+
// }
1111

1212
// =============> write the error message here
1313
//Uncaught SyntaxError SyntaxError: Unexpected number

Sprint-2/5-stretch-extend/format-time.js

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,30 @@
33
// Your task is to write tests for as many different groups of input data or edge cases as you can, and fix any bugs you find.
44

55
function formatAs12HourClock(time) {
6-
const hours = Number(time.slice(0, 2));
7-
if (hours > 12) {
8-
return `${hours - 12}:00 pm`;
6+
const hours = Number(time.slice(0, 2));
7+
const minutes = time.slice(3, 5);
8+
let ampm = "am";
9+
let newHours = hours;
10+
11+
if (hours === 0) {
12+
newHours = 12; // midnight
13+
} else if (hours === 12) {
14+
ampm = "pm"; // noon
15+
} else if (hours > 12) {
16+
newHours = hours - 12;
17+
ampm = "pm";
918
}
10-
return `${time} am`;
11-
}
12-
13-
const currentOutput = formatAs12HourClock("08:00");
14-
const targetOutput = "08:00 am";
15-
console.assert(
16-
currentOutput === targetOutput,
17-
`current output: ${currentOutput}, target output: ${targetOutput}`
18-
);
19-
20-
const currentOutput2 = formatAs12HourClock("23:00");
21-
const targetOutput2 = "11:00 pm";
22-
console.assert(
23-
currentOutput2 === targetOutput2,
24-
`current output: ${currentOutput2}, target output: ${targetOutput2}`
25-
);
26-
27-
console.log(formatAs12HourClock("23:14"))
28-
console.log(formatAs12HourClock("00:00"))
29-
console.log(formatAs12HourClock("00:01"))
30-
console.log(formatAs12HourClock("11:59"))
31-
console.log(formatAs12HourClock("12:00"))
32-
console.log(formatAs12HourClock("12:01"))
33-
console.log(formatAs12HourClock("13:45"))
34-
35-
36-
3719

20+
const showHours = newHours.toString().padStart(2, "0");
21+
return `${showHours}:${minutes} ${ampm}`;
22+
}
3823

24+
// Now let's test different times
3925

26+
console.log(formatAs12HourClock("00:00"));
27+
console.log(formatAs12HourClock("00:01"));
28+
console.log(formatAs12HourClock("08:00"));
29+
console.log(formatAs12HourClock("11:59"));
30+
console.log(formatAs12HourClock("12:00"));
31+
console.log(formatAs12HourClock("13:45"));
32+
console.log(formatAs12HourClock("23:14"));

0 commit comments

Comments
 (0)