1. function deserializeTimeValues(timeStrings) {
  2. const parsedTimes = [];
  3. for (const timeString of timeStrings) {
  4. try {
  5. // Attempt to parse the time string using a regular expression.
  6. const match = timeString.match(/^(\d{2}):(\d{2})$/);
  7. if (match) {
  8. const hours = parseInt(match[1], 10);
  9. const minutes = parseInt(match[2], 10);
  10. if (hours >= 0 && hours <= 23 && minutes >= 0 && minutes <= 59) {
  11. parsedTimes.push({ hours, minutes });
  12. } else {
  13. throw new Error("Invalid time format: Hours must be between 0-23 and minutes between 0-59.");
  14. }
  15. } else {
  16. // Handle cases where the time string doesn't match the expected format.
  17. throw new Error("Invalid time format: Please use HH:MM format.");
  18. }
  19. } catch (error) {
  20. // Catch any errors during parsing and store an error message.
  21. parsedTimes.push(`Error: ${error.message}`);
  22. }
  23. }
  24. return parsedTimes;
  25. }

Add your comment