DEV Community

Discussion on: Daily Challenge #267 - Braking Speed

Collapse
dry profile image
Hayden Mankin

Is there something about this problem I'm missing? This seems like it's just a math problem, is there some trick I'm missing that makes it faster? Or not lose precision? Or am I missing an edge case some how?

anyways, here's a javascript solution.

const g = 9.81; // gravity in m/s
const k = 3600 / 1000; // conversion coefficient: m/s to km/hr

const dist = (v, mu, t=1) => {
  v = v / k; 
  return v * t + v * v / (mu * 2 * g); 
}

const speed = (d, mu, t=1) => {
  return k * (Math.sqrt(g * mu * (2 * d + g * t * t * mu)) - g * t * mu);
}

console.log(dist(144, 0.3)) // 311.83146449201496
console.log(dist(92, 0.5)) // 92.12909477605365
console.log(speed(159, 0.8)) // 153.79671564846308
console.log(speed(153, 0.7)) // 142.14404997566152