|
3 | 3 | // Your task is to write tests for as many different groups of input data or edge cases as you can, and fix any |
4 | 4 | // bugs you find. |
5 | 5 |
|
| 6 | +// This is the latest solution to the problem from the prep. |
| 7 | +// Make sure to do the prep before you do the coursework |
| 8 | +// Your task is to write tests for as many different groups of input data or edge cases as you can, and fix any |
| 9 | +// bugs you find. |
| 10 | + |
6 | 11 | function formatAs12HourClock(time) { |
7 | | - if (Number(time.slice(0, 2)) === 12) { |
8 | | - return `${time} pm` // if time is "12:00" it will show |
| 12 | + const hours = Number(time.slice(0, 2)); // "hours" variable represents first two digits of the time string turned into number |
| 13 | + const minutes = Number(time.slice(-2));// "mninutes" variable represents last two digits of the "time" string converted to number |
| 14 | + if (hours === 12) { |
| 15 | + return `${time}pm` // if time is "12:00" it will show |
9 | 16 | //12:00 pm |
10 | | - } else if (Number(time.slice(0, 2))=== 24) { |
11 | | - return `00:00 am`; |
12 | | - } else if (Number(time.slice(0, 2)) > 12) { |
13 | | - return `${Number(time.slice(0, 2)) - 12}:00 pm`; |
| 17 | + } else if (hours === 24 && time.slice(-2) === "00") { |
| 18 | + return `00:00am`; |
| 19 | + } else if (hours === 24 && minutes > 0) { |
| 20 | + return `00:${minutes}am` |
| 21 | + |
| 22 | + }else if (hours > 12) { |
| 23 | + return `${hours - 12}:${minutes}pm`; |
14 | 24 | } |
15 | | - return `${time} am`; |
| 25 | + return `${time}am`; |
16 | 26 | } |
17 | 27 | const currentOutput = formatAs12HourClock("08:00"); |
18 | | -const targetOutput = "08:00 am"; |
| 28 | +const targetOutput = "08:00am"; |
19 | 29 | console.assert( |
20 | 30 | currentOutput === targetOutput, |
21 | 31 | `current output: ${currentOutput}, target output: ${targetOutput}` |
22 | 32 | ); |
23 | 33 |
|
24 | | -const currentOutput2 = formatAs12HourClock("23:00"); |
25 | | -const targetOutput2 = "11:00 pm"; |
| 34 | +const currentOutput2 = formatAs12HourClock("23:35"); |
| 35 | +const targetOutput2 = "11:35pm"; |
26 | 36 | console.assert( |
27 | 37 | currentOutput2 === targetOutput2, |
28 | | - `current output: ${currentOutput2}, target output: ${targetOutput2}` |
| 38 | + `current output2: ${currentOutput2}, target output2: ${targetOutput2}` |
29 | 39 | ); |
30 | 40 | const currentOutput3 = formatAs12HourClock("00:00");// |
31 | | -const targetOutput3 = "00:00 am";// expecting new test to return "00:00 am" |
| 41 | +const targetOutput3 = "00:00am";// expecting new test to return "00:00 am" |
32 | 42 | console.assert( |
33 | 43 | currentOutput3 === targetOutput3, |
34 | | - `current output: ${currentOutput3}, target output: ${targetOutput3}` |
| 44 | + `current output3: ${currentOutput3}, target output3: ${targetOutput3}` |
35 | 45 | ); // if currentOutput3 and targetOutput3 do not match, the assertion message will be given. |
36 | 46 |
|
37 | 47 | const currentOutput4 = formatAs12HourClock("12:00");// |
38 | | -const targetOutput4 = "12:00 pm"; // expected output for "12:00" is "12:00 pm" |
| 48 | +const targetOutput4 = "12:00pm"; // expected output for "12:00" is "12:00 pm" |
39 | 49 | console.assert( |
40 | 50 | currentOutput4 === targetOutput4, |
41 | | - `current output: ${currentOutput4}, target output: ${targetOutput4}` |
| 51 | + `current output4: ${currentOutput4}, target output4: ${targetOutput4}` |
42 | 52 | );// if currentOutput and targetOutput do not match, the assertion message will be given. |
43 | 53 | const currentOutput5 = formatAs12HourClock("24:00"); |
44 | | -const targetOutput5 = "00:00 am"; |
| 54 | +const targetOutput5 = "00:00am"; |
45 | 55 | console.assert( |
46 | 56 | currentOutput5 === targetOutput5, |
47 | 57 | `current output5: ${currentOutput5}, target output5: ${targetOutput5}` |
48 | 58 | ); // if 24:00 is entered and the output is not "00:00 am" the assertion message will be given |
49 | 59 |
|
| 60 | +// below is test to try with minutes |
| 61 | +const currentOutput6 = formatAs12HourClock("22:55"); |
| 62 | +const targetOutput6 = "10:55pm"; |
| 63 | +console.assert(currentOutput6 === targetOutput6, `current outpu6: ${currentOutput6}, target output6: ${targetOutput6}`); |
| 64 | + |
| 65 | +// following test tests what happens if time passed 24:00 hours. |
| 66 | +const currentOutput7 = formatAs12HourClock("24:25"); |
| 67 | +const targetOutput7 = "00:25am"; |
| 68 | +console.assert(currentOutput7 === targetOutput7, `current output7: ${currentOutput7}, target output: ${targetOutput7}`) |
| 69 | + |
| 70 | + |
| 71 | + |
50 | 72 |
|
0 commit comments