Using JavaScript to Avoid a Train Wreck

Demonstrating the value of functions by determining the timing of a train wreck

Understanding the Problem

Distance = Rate × Time
Time = Distance ÷ Rate

Defining a Function

function name(parameters) {
// instructions to execute
}
function timeToIntercept(distance, speedA, speedB) {
// instructions to execute
}
function timeToIntercept(distance, speedA, speedB) {
var relativeSpeed = speedA + speedB;
return distance / relativeSpeed;
}
timeToIntercept(150, 80, 120);
0.75

The Value of Programmatic Functions

--

--

Designer and developer who occasionally writes.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store