如何解决秒表圈比较
我用HTML,CSS和Vanilla JS创建了一个秒表Web应用程序。计时功能很好,它的计时和停止功能很好,可以打印出一个在主计时器下运行的单独的计时计时器。
现在,当我尝试获得最高和最低单圈时间时,我的问题浮出水面。我能够在一定程度上获得这些值,但在某些情况下并不完全准确。例如,当您分别圈速00:02:03和00:00:42时,由于'03低于'42,我的代码选择了第一次作为最低圈速。选择最高圈时也会发生同样的情况。
我正在尝试存储特定单圈时间的索引,然后继续检查最低毫秒数,例如,单圈时间要比两个短。我有一个函数,但是如果有更多的话,它只会返回一个索引号。
class DoUntilStable:
def __init__(self):
self._stable = False
def __iter__(self):
while not self._stable:
self._stable = True
yield self
def changed(self):
self._stable = False
for dus in DoUntilStable():
for rect2 in list(rects):
if rect.overlaps(rect2):
dus.changed()
rect = rect.merge(rect2)
rects.remove(rect2)
解决方法
我有一个可以实现的示例示例,而不是弄乱您的代码。
首先,我从大腿上剥离结肠。然后,我只使用Math.min和Math.max从数组中找到最小值/最大值。
这似乎是一个奇怪的解决方法,但是如果没有冒号,它们将变成正常数字。
laps = ["00:02:03","00:00:42"]; //an array of saved lap times
lap_times = {}; //an empty object that will hold the Numerical Lap time as a key and the string version as the value ie: lap_times[42] = "00:00:42" that way we can grab the original time after determining min/max
laps = laps.map(function(lapTime){//loops through the array and returns a modified value back in each values place
lap = Number(lapTime.replace(/:/g,"")); //replaces the colons and converts to a Number
lap_times[lap] = lapTime //sets the converted time as the key and original time as the value
return lap;
});
/* ... is called spread/rest syntax https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax
Without using lap_times[],min/max would return the numerical representation of the time. So by adding it,it will pull back the previously created full string value
*/
slowest = lap_times[Math.max(...laps)];
fastest = lap_times[Math.min(...laps)];
console.log("slowest",slowest,"fastest",fastest)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。