My problem is a wrong result. I have a function this function has a distance and a time. The result I want to achieve is calculating running speed.
function runningPace(distance, time) {
let result = time.replace(":", ".")
let fl = parseFloat(result)
let calc = fl / distance
let num = calc.toFixed(2) + ''
if(num.length === 1) {
num = num + '.00'
}
let rep = num.replace('.', ':')
console.log(distance, time, result, fl, calc, rep, num)
return rep;
}
console.log(runningPace(5, '25:00')) // '5:00'
console.log(runningPace(4.99, '22:32')) // '4:30'
I wrote such a function. Sorry for the naming, I'll fix it when I get it fixed.
When I test this code, the output is:
expected '4:47' to equal '4:30'
'4:30' => four minute thirty second
How can I find a solution? Thanks everyone in advance.
convert anything in seconds:
function runningPace(dist, time)
{
let
[ mn, sc ] = time.split(':').map(Number)
, fl = Math.floor(((mn *60) + sc) / dist)
;
sc = fl % 60
mn = (fl - sc) / 60
return `${mn}:${(sc<9)?'0'+sc:sc}`
}
console.log(runningPace( 5, '25:00')) // 5:00
console.log(runningPace( 4.99, '22:32')) // 4:30