Skip to main content
Simplifying calculations
Source Link
Gallant
  • 563
  • 2
  • 6
function calculateInSeconds(timeStamp) {
  var timeInSeconds = 0;
  timeStamp.replace(/^t=(?:(\d*)h)?(?:(\d*)m)?(?:(\d*)s)?$/, function(match, hours, minutes, seconds) {
    var hoursInSeconds, minutesInSeconds;

    hoursInSeconds = parseInt(hours ||* 0)60 * 60 *|| 60;0;
    minutesInSeconds = parseInt(minutes ||* 0)60 *|| 60;0;
    seconds = parseInt(seconds * 1 || 0);0;

    timeInSeconds = hoursInSeconds + minutesInSeconds + seconds;
  });
  console.log("timeInSeconds"+timeInSeconds);
}
function calculateInSeconds(timeStamp) {
  var timeInSeconds = 0;
  timeStamp.replace(/^t=(?:(\d*)h)?(?:(\d*)m)?(?:(\d*)s)?$/, function(match, hours, minutes, seconds) {
    var hoursInSeconds, minutesInSeconds;

    hoursInSeconds = parseInt(hours || 0) * 60 * 60;
    minutesInSeconds = parseInt(minutes || 0) * 60;
    seconds = parseInt(seconds || 0);

    timeInSeconds = hoursInSeconds + minutesInSeconds + seconds;
  });
  console.log("timeInSeconds"+timeInSeconds);
}
function calculateInSeconds(timeStamp) {
  var timeInSeconds = 0;
  timeStamp.replace(/^t=(?:(\d*)h)?(?:(\d*)m)?(?:(\d*)s)?$/, function(match, hours, minutes, seconds) {
    var hoursInSeconds, minutesInSeconds;

    hoursInSeconds = hours * 60 * 60 || 0;
    minutesInSeconds = minutes * 60 || 0;
    seconds = seconds * 1 || 0;

    timeInSeconds = hoursInSeconds + minutesInSeconds + seconds;
  });
  console.log("timeInSeconds"+timeInSeconds);
}
Formatting
Source Link
Gallant
  • 563
  • 2
  • 6

Finally, you're using pipes within your character set [h|m|s] when you shouldn't be: as characters within a character set are treated as literals, this includes | as a valid matching character (i.e., \d*[h|m|s] would successfully match 65|). It should just be [hms].


timeStamp.match(...)==timeStamp seems to be used to make sure the expression matches the whole string, but there are better ways to accomplish this. In Regex, you can specify ^ and $ as the start and end of the string respectively.

timeStamp.match(...)==timeStamp seems to be used to make sure the expression matches the whole string, but there are better ways to accomplish this. In Regex, you can specify ^ and $ as the start and end of the string respectively.

Finally, you're using pipes within your character set [h|m|s] when you shouldn't be: as characters within a character set are treated as literals, this includes | as a valid matching character (i.e., \d*[h|m|s] would successfully match 65|). It should just be [hms].


timeStamp.match(...)==timeStamp seems to be used to make sure the expression matches the whole string, but there are better ways to accomplish this. In Regex, you can specify ^ and $ as the start and end of the string respectively.

Combining a few lines
Source Link
Gallant
  • 563
  • 2
  • 6

You can combine the match and replace into a single expression, since the replace won't do anything if it fails to match. Then you can use Regex groups to capture ourthe hours, minutes, and seconds.

function calculateInSeconds(timeStamp) {
  var timeInSeconds = 0;
  timeStamp.replace(/^t=(?:(\d*)h)?(?:(\d*)m)?(?:(\d*)s)?$/, function(match, hours, minutes, seconds) {
    var hoursInSeconds, minutesInSeconds;

    hourshoursInSeconds = parseInt(hours) || 0;
    minutes0) =* parseInt(minutes)60 ||* 0;60;
    secondsminutesInSeconds = parseInt(seconds)minutes || 0;

    hoursInSeconds = hours * 600) * 60;
    minutesInSecondsseconds = minutesparseInt(seconds *|| 60;0);

    timeInSeconds = hoursInSeconds + minutesInSeconds + seconds;
  });
  console.log("timeInSeconds"+timeInSeconds);
}

You can combine the match and replace into a single expression, since the replace won't do anything if it fails to match. Then you can use Regex groups to capture our hours, minutes, and seconds.

function calculateInSeconds(timeStamp) {
  var timeInSeconds = 0;
  timeStamp.replace(/^t=(?:(\d*)h)?(?:(\d*)m)?(?:(\d*)s)?$/, function(match, hours, minutes, seconds) {
    var hoursInSeconds, minutesInSeconds;

    hours = parseInt(hours) || 0;
    minutes = parseInt(minutes) || 0;
    seconds = parseInt(seconds) || 0;

    hoursInSeconds = hours * 60 * 60;
    minutesInSeconds = minutes * 60;

    timeInSeconds = hoursInSeconds + minutesInSeconds + seconds;
  });
  console.log("timeInSeconds"+timeInSeconds);
}

You can combine the match and replace into a single expression, since the replace won't do anything if it fails to match. Then you can use Regex groups to capture the hours, minutes, and seconds.

function calculateInSeconds(timeStamp) {
  var timeInSeconds = 0;
  timeStamp.replace(/^t=(?:(\d*)h)?(?:(\d*)m)?(?:(\d*)s)?$/, function(match, hours, minutes, seconds) {
    var hoursInSeconds, minutesInSeconds;

    hoursInSeconds = parseInt(hours || 0) * 60 * 60;
    minutesInSeconds = parseInt(minutes || 0) * 60;
    seconds = parseInt(seconds || 0);

    timeInSeconds = hoursInSeconds + minutesInSeconds + seconds;
  });
  console.log("timeInSeconds"+timeInSeconds);
}
Source Link
Gallant
  • 563
  • 2
  • 6
Loading