Have the javascript function CountingMinutes(str) take the str parameter being passed which will be two times (each properly formatted with a colon and am or pm) separated by a hyphen and return the total number of minutes between the two times. The time will be in a 12 hour clock format. For example: if str is 9:00am-10:00am then the output should be 60. If str is 1:00pm-11:00am the output should be 1320.
function CountingMinutes(str) {
// code goes here
return str;
}
// keep this function call here
console.log(CountingMinutes(readline()));

Respuesta :

Using the knowledge in computational language in Java it is possible to write a code that function as CountingMinutes:

Writing the code in Java:

function CountingMinutes(str) {

     // code goes here  

     // Declare variables for calculating difference in minutes

     // variables in JavaScript are declared with "let" keyword

     /* Build regelar expression which will match the pattern of "12houttime-12hourtime"

        and extract time or hours in numbers from it */

     let extractedTimeStringArray = str.match(/(\d+)\:(\d+)(\w+)-(\d+)\:(\d+)(\w+)/);

     // extractedTimeStringArray array will be like- ["1:00pm-11:00am", "1", "00", "pm", "11", "00", "am", index: 0, input: "1:00pm-11:00am", groups: undefined]    for str = "1:00pm-11:00am"

     // console.log('object', time)

     

     // Extract array value at 1st index for getting first time's hours (ie time before hyphen like 1:00pm in 1:00pm-11:00am )  (like 11 from 11:32am) and convert them to minutes by multiplying by 60

     let mintsOfFirstTimeFromHours = extractedTimeStringArray[1] * 60;

     // Extract array value at 2nd index for getting first time's minutes like 32 from 11:32am

     let mintsOfFirstTimeFromMints = extractedTimeStringArray[2];

     // Extract array value at 4th index for getting second time's hours (ie time after hyphen like 11:00am in 1:00pm-11:00am ) and convert them to minutes by multiplying by 60

     let mintsOfSecondTimeFromHours = extractedTimeStringArray[4] * 60;

     // Extract array value at 5th index for getting second time's minutes like 32 from 11:32am

     let mintsOfSecondTimeFromMints = extractedTimeStringArray[5];

     // if second time's 12 hour time is in pm

     if (extractedTimeStringArray[6] === "pm") {

       // Add 12 * 60 = 720 minutes for 12 hrs

         mintsOfSecondTimeFromHours += 720;

     }

     // if first time's 12 hour time is in pm

     if (extractedTimeStringArray[3] === "pm") {

        // Add 12 * 60 = 720 minutes for 12 hrs to first time

       mintsOfFirstTimeFromHours += 720;

        // Add 12 * 60 *2 = 1440 minutes for 24 hrs to second time

       mintsOfSecondTimeFromHours += 1440;

     }

     // Calculate output minutes difference between two times separated by hyphen

    str = (mintsOfSecondTimeFromHours - mintsOfFirstTimeFromHours) + (mintsOfSecondTimeFromMints - mintsOfFirstTimeFromMints);

     // return calculated minutes difference

     return str;

   }

   // keep this function call here

   // call the function and console log the result

   console.log(CountingMinutes("1:00pm-11:00am"));

   // output in console will be-  1320

See more about Java at: brainly.com/question/12975450

#SPJ1

Ver imagen lhmarianateixeira
Ver imagen lhmarianateixeira