I am currently working on a stopwatch project where I log the time into an array. However, when I try to use Math.min(array) or Math.max(array), it returns NaN (not a number). The time format for the stopwatch is like 00:00:15.91 which is not recognized as a valid number. Is there a way to work around this issue?
To simplify, I store times like "00:00:15.91" in an array and need to retrieve the largest and smallest numbers. Is there a solution for this in JavaScript without using any libraries?
Note: While I don't think sharing my code is necessary for this question, please let me know if you would like to see it.
else{
let displayTime = displayHours + ":" + displayMinutes + ":" + displaySeconds + "." + displayMilliseconds;
let intTime = displayHours + displayMinutes + displaySeconds + displayMilliseconds;
let times = document.getElementById("times");
window.clearInterval(interval);
status = "stopped";
if (status == "stopped") {
times.innerHTML += displayTime + "<img src='https://img.icons8.com/windows/32/000000/xbox-x.png'/><br/>";
allTimes.push(Number(intTime));
if (allTimes.length == 0) {
document.getElementById("bestTime").innerHTML = "none";
document.getElementById("worstTime").innerHTML = "none";
document.getElementById("average").innerHTML = "none";
document.getElementById("stats").innerHTML = "none";
}
if (allTimes.length != 0) {
document.getElementById("bestTime").innerHTML = Math.min(...allTimes);
}
}
}
Edit: Added my code snippet