Programming Questions

  • Newest
  • Popular Tags
  • Ask Question
  • Counting Minutes I - JavaScript
    I cant figure this one out, can anyone tell me what I am missing or help me make it work. function CountingMinutesI(str) { var times = str.split('-'); var from = times[0]; var to = times[1]; function timer(num){ var hrs = num.split(':'); var min = hrs[1].slice(0,2); var ampm = hrs[1].slice(2); var totalMins = 0; var pmMins = if (ampm === 'pm'){ return 720; }else return 0; var totalMins = hrs*60 + mins + pmMins ; return totalMins; } var diff = timer(from) - timer(to); return diff; } // keep this function call here CountingMinutesI("12:30pm-12:00am");
    fearlessinc posted this question on 5/24/16 | javascript, counting, minutes
    Answers
  • +
  • 0
  • -
  • You missed converting some data types so I make a few edits to make your code better. function CountingMinutesI(str) { var times = str.split('-'); var from = times[0]; var to = times[1]; function timer(num){ var hrs = num.split(':'); var min = hrs[1].slice(0,2); var ampm = hrs[1].slice(2); var totalMins = 0; var pmMins = ampm === 'pm' ? 720 : 0; var totalMins = Number(hrs[0])*60+ Number(min) + pmMins ; return totalMins; } var diff = timer(from) - timer(to); return diff; } CountingMinutesI("12:30pm-12:00am");
    Log in to write an answer.