|  | 
| 1 |  | -// This is the latest solution to the problem from the prep. | 
| 2 |  | -// Make sure to do the prep before you do the coursework | 
| 3 |  | -// 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. | 
|  | 1 | +// // This is the latest solution to the problem from the prep. | 
|  | 2 | +// // Make sure to do the prep before you do the coursework | 
|  | 3 | +// // 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 | 4 | 
 | 
|  | 5 | +// function formatAs12HourClock(time) { | 
|  | 6 | +//   const hours = Number(time.slice(0, 2)); | 
|  | 7 | +//   if (hours > 12) { | 
|  | 8 | +//     return `${hours - 12}:00 pm`; | 
|  | 9 | +//   } | 
|  | 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 | +// // ======= TESTS ======= | 
|  | 28 | + | 
|  | 29 | +// // Before midday | 
|  | 30 | +// console.log(formatAs12HourClock("08:00")); // 08:00 am | 
|  | 31 | +// console.log(formatAs12HourClock("09:45")); // 09:45 am | 
|  | 32 | + | 
|  | 33 | +// // Midday / Noon | 
|  | 34 | +// console.log(formatAs12HourClock("12:00")); // 12:00 pm | 
|  | 35 | +// console.log(formatAs12HourClock("12:30")); // 12:30 pm | 
|  | 36 | + | 
|  | 37 | +// // After midday | 
|  | 38 | +// console.log(formatAs12HourClock("14:15")); // 02:15 pm | 
|  | 39 | +// console.log(formatAs12HourClock("23:00")); // 11:00 pm | 
|  | 40 | + | 
|  | 41 | +// // Midnight | 
|  | 42 | +// console.log(formatAs12HourClock("00:00")); // 12:00 am | 
|  | 43 | +// console.log(formatAs12HourClock("00:30")); // 12:30 am | 
|  | 44 | + | 
|  | 45 | +// Function to convert 24-hour time to 12-hour time | 
| 5 | 46 | function formatAs12HourClock(time) { | 
| 6 |  | -  const hours = Number(time.slice(0, 2)); | 
| 7 |  | -  if (hours > 12) { | 
| 8 |  | -    return `${hours - 12}:00 pm`; | 
|  | 47 | +  // Split the time into hours and minutes | 
|  | 48 | +  let parts = time.split(":"); | 
|  | 49 | +  let hours = Number(parts[0]); | 
|  | 50 | +  let minutes = parts[1]; | 
|  | 51 | + | 
|  | 52 | +  // Decide if it's am or pm | 
|  | 53 | +  let suffix = hours >= 12 ? "pm" : "am"; | 
|  | 54 | + | 
|  | 55 | +  // Convert hours to 12-hour format | 
|  | 56 | +  if (hours === 0) { | 
|  | 57 | +    hours = 12; // midnight | 
|  | 58 | +  } else if (hours > 12) { | 
|  | 59 | +    hours = hours - 12; // afternoon | 
| 9 | 60 |   } | 
| 10 |  | -  return `${time} am`; | 
|  | 61 | + | 
|  | 62 | +  // Make sure hours are always 2 digits | 
|  | 63 | +  let hoursStr = hours.toString().padStart(2, "0"); | 
|  | 64 | + | 
|  | 65 | +  // Return the formatted time | 
|  | 66 | +  return hoursStr + ":" + minutes + " " + suffix; | 
| 11 | 67 | } | 
| 12 | 68 | 
 | 
| 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 |  | -); | 
|  | 69 | +// ======= TESTS ======= | 
|  | 70 | + | 
|  | 71 | +// Before midday | 
|  | 72 | +console.log(formatAs12HourClock("08:00")); | 
|  | 73 | +console.log(formatAs12HourClock("09:45")); | 
|  | 74 | + | 
|  | 75 | +// Midday / Noon | 
|  | 76 | +console.log(formatAs12HourClock("12:00")); | 
|  | 77 | +console.log(formatAs12HourClock("12:30")); | 
|  | 78 | + | 
|  | 79 | +// After midday | 
|  | 80 | +console.log(formatAs12HourClock("14:15")); | 
|  | 81 | +console.log(formatAs12HourClock("23:00")); | 
|  | 82 | + | 
|  | 83 | +// Midnight | 
|  | 84 | +console.log(formatAs12HourClock("00:00")); | 
|  | 85 | +console.log(formatAs12HourClock("00:30")); | 
0 commit comments