Skip to content

Commit e6f6333

Browse files
author
lintsang
committed
finished exercises in 4-mandatory-interpret and 5-stretch-extend folders
1 parent fecfbc2 commit e6f6333

2 files changed

Lines changed: 45 additions & 7 deletions

File tree

Sprint-2/4-mandatory-interpret/time-format.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,18 @@ function formatTimeDisplay(seconds) {
2121
// Questions
2222

2323
// a) When formatTimeDisplay is called how many times will pad be called?
24-
// =============> write your answer here
24+
// =============> It will be called 3 times in line 15.
2525

2626
// Call formatTimeDisplay with an input of 61, now answer the following:
2727

2828
// b) What is the value assigned to num when pad is called for the first time?
29-
// =============> write your answer here
29+
// =============> 0 will be assigned to num when pad is called for the first time.
3030

3131
// c) What is the return value of pad is called for the first time?
32-
// =============> write your answer here
32+
// =============> 00 will be the return value when pad is called the first time.
3333

3434
// d) What is the value assigned to num when pad is called for the last time in this program? Explain your answer
35-
// =============> write your answer here
35+
// =============> When pad is being called for the last time, it is on line 15 'pad(remainingSeconds)'. So a value of 1 will be assigned to it as remainingSeconds in line 10 was declared as 1.
3636

3737
// e) What is the return value of pad when it is called for the last time in this program? Explain your answer
38-
// =============> write your answer here
38+
// =============> When pad is being called for the last time, it is on line 15 'pad(remainingSeconds)'. So a value of 1 will be assigned to function pad(num) where num is in value of 1. In line 3, as this string is smaller than 2 which means it is only in 1 digit, we add a '0' in front of it. So it return a numString '01' to the line 15.

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

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
11
// This is the latest solution to the problem from the prep.
22
// Make sure to do the prep before you do the coursework
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.
4+
function pad(num) {
5+
let numString = num.toString();
6+
while (numString.length < 2) {
7+
numString = "0" + numString;
8+
}
9+
return numString;
10+
}
411

512
function formatAs12HourClock(time) {
613
const hours = Number(time.slice(0, 2));
14+
const minute = pad(Number(time.slice(3,5)));
715
if (hours > 12) {
8-
return `${hours - 12}:00 pm`;
16+
return `${hours - 12}:${minute} pm`;
17+
} else if (hours == 12 & minute == '00'){
18+
return `${time} pm`;
919
}
1020
return `${time} am`;
1121
}
@@ -21,5 +31,33 @@ const currentOutput2 = formatAs12HourClock("23:00");
2131
const targetOutput2 = "11:00 pm";
2232
console.assert(
2333
currentOutput2 === targetOutput2,
24-
`current output: ${currentOutput2}, target output: ${targetOutput2}`
34+
`2current output: ${currentOutput2}, target output: ${targetOutput2}`
2535
);
36+
37+
const currentOutput3 = formatAs12HourClock("23:59");
38+
const targetOutput3 = "11:59 pm";
39+
console.assert(
40+
currentOutput3 === targetOutput3,
41+
`current output: ${currentOutput3}, target output: ${targetOutput3}`
42+
);
43+
44+
const currentOutput4 = formatAs12HourClock("01:31");
45+
const targetOutput4 = "01:31 am";
46+
console.assert(
47+
currentOutput4 === targetOutput4,
48+
`current output: ${currentOutput4}, target output: ${targetOutput4}`
49+
);
50+
51+
const currentOutput5 = formatAs12HourClock("00:00");
52+
const targetOutput5 = "00:00 am";
53+
console.assert(
54+
currentOutput5 === targetOutput5,
55+
`current output: ${currentOutput5}, target output: ${targetOutput5}`
56+
);
57+
58+
const currentOutput6 = formatAs12HourClock("12:00");
59+
const targetOutput6 = "12:00 pm";
60+
console.assert(
61+
currentOutput6 === targetOutput6,
62+
`current output: ${currentOutput6}, target output: ${targetOutput6}`
63+
);

0 commit comments

Comments
 (0)