Hoe te maken: Snelheidsconversie

Leer hoe je een snelheidsconversieconverter maakt met HTML en JavaScript.

Snelheidsconversie

Voer een waarde in in elk veld om te converteren tussen snelheidsmaatstaven:

Create a speed converter

Create an input element that can convert one unit of speed to another.

Step 1 - Add HTML:

<p>
  <label>MPH</label>
  <input id="inputMPH" type="number" placeholder="MPH"
  oninput="speedConverter(this.value)"
  onchange="speedConverter(this.value)"
</p>
<p>KPH: <span id="outputKPH"></span></p>

Step 2 - Add JavaScript:

Convert miles per hour to kilometers per hour:

/* When the input field receives input, convert the value from mph to kph */
function speedConverter(valNum) {
  valNum = parseFloat(valNum);
  document.getElementById("outputKPH").innerHTML = valNum * 1.609344;
}

Try it yourself

Convert MPH to other units

The following table shows how to convert MPH to other units of speed:

Description Formula Example
Convert MPH to KPH KPH = MPH * 1.609344 Try it
Convert MPH to knots knots = MPH / 1.150779 Try it
Convert MPH to Mach Mach = MPH / 761.207 Try it

Convert KPH to other units

The following table shows how to convert KPH to other units of speed:

Description Formula Example
Convert KPH to MPH MPH = KPH / 1.609344 Try it
Convert KPH to knots knots = KPH / 1.852 Try it
Convert KPH to Mach Mach = KPH / 1225.044 Try it

Convert knots to other units

The following table shows how to convert knots to other units of speed:

Description Formula Example
Convert knots to MPH MPH = knots * 1.150779 Try it
Convert knots to KPH KPH = knots * 1.852 Try it
Convert knots to Mach Mach = knots / 661.4708 Try it

Convert Mach to other units

The following table shows how to convert Mach numbers to other units of speed:

Description Formula Example
Convert Mach to MPH MPH = Mach * 761.207 Try it
Convert Mach to KPH KPH=Mach*1225.044 Try it
Convert Mach to knots knots=Mach*661.4708 Try it