Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Problem 5: parse a time duration Time duration is defined as a length of time expressed in hours, minutes, and seconds. An example, the duration

image text in transcribedimage text in transcribedimage text in transcribed
Problem 5: parse a time duration Time duration is defined as a length of time expressed in hours, minutes, and seconds. An example, the duration from 9:15 AM to 10:45 AM is: Hours: 1 Minutes: 30 Seconds: 0 A dataset includes thousands of time duration, stored as strings. However, over the years, different authors have used slightly different formats. All of the following are valid and need to be parsed: 1. "1:30:0" 2. "1h30m05" * In the first case, the values are separated by " :" . I In the second, the values are followed by the unit (h, m, s). * Valid Hour values are positive integers between @ and 24. Valid Minute values are positive integers between 0 and 59. Valid Second values are positive integers between 0 and 59. If the input duration is invalid, return the value "null" . Parse the value and return a new string in the following form: "(hours, minutes, seconds)" @param {string} value - a time duration string in one of the given forms @returns {string|null} - a time duration formatted as "(hours, minutes, seconds)" or *null" if the duration isn't valid/recognizedfunction normalizeDuration (value) { // check if the input value is valid if (value === null | | value === undefined | | typeof value !== 'string') { return null; // match the hours, minutes, and seconds in the input value const match = value. match(/(\\d+) (?: : [h) (\\d+) (?: : |m) (\\d+)s?/); if (!match) { |return null; // extract the hours, minutes, and seconds from the match const hours = parseInt(match [1], 10); const minutes = parseInt(match[2], 10); const seconds = parseInt(match [3], 10); // check if the hours, minutes, and seconds are valid if (hours 24 | | minutes 59 | | seconds 59) { return null; // return the formatted duration return *(${hours), ${minutes}, ${seconds})";Problem 5 - normalizeDuration( ) function > the hour value of a duration must be greater than 0 expect (received) . toBe (expected) // Object. is equality Expected: null Received: " (1, 2, 3)" 31 32 test (' the hour value of a duration must be greater than 0', function ( ) { > 33 expect ( normalizeDuration( ' -1:2:3' ) ) . toBe(null); A 34 expect ( normalizeDuration( ' -1h2m3s' ) ) . toBe (null) ; 35 }); 36 at Object . toBe (src/problem-05 . test . js: 33:41)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Mobile Communications

Authors: Jochen Schiller

2nd edition

978-0321123817, 321123816, 978-8131724262

Students also viewed these Programming questions

Question

What is the drawback of the wider and deeper neural network?

Answered: 1 week ago