0

I need some help finishing the javascript for a form where users input numbers and the numbers are put into an equation and solved. I am pretty sure I have all of the HTML done correctly but the Javascript code is throwing me.

Any help in figuring this out is appreciated. I have hard time understanding Javascript all together.

function calculateMph() {
  var feet = document.getElementById("inputFeet").value; //distance in feet
  var time = document.getElementById("inputSeconds").value; //speed in seconds
  var mph = (45 * feet) / (22 * time); //the equation here

  document.write("<p>Your speed in mph is " + mph + "</p>");
}
<h1>Lab: Chapter Two</h1>
<h3>Fill in the form to determine your speed in miles per hour for a particular race that you ran:</h3>
<p><strong>MPH=(15f/22t) <br></strong>where f=distance run in feet and t is time in seconds.</p>
<form id="form">
  Distance in Feet(f):<input id="inputFeet" type="text" name="feet" required><br> Number of Seconds(t):<input id="inputSeconds" type="text" name="seconds" required><br><br>
  <input type="button" onclick="calculateMph()" value="Calculate" />
</form>
<p><b>Your speed in MPH:</b><br>
  <span id="result"></span>
</p>
mplungjan
  • 169,008
  • 28
  • 173
  • 236

2 Answers2

0

Just change your code

document.write("<p>Your speed in mph is " + mph + "</p>");

with this

document.getElementById("result").innerHTML = mph.toFixed(2);

function calculateMph() {
  var feet = document.getElementById("inputFeet").value; //distance in feet
  var time = document.getElementById("inputSeconds").value; //speed in seconds
  var mph = (45 * feet) / (22 * time); //the equation here

  document.getElementById("result").innerHTML = mph.toFixed(2);
}
<h1>Lab: Chapter Two</h1>
<h3>Fill in the form to determine your speed in miles per hour for a particular race that you ran:</h3>
<p><strong>MPH=(15f/22t) <br></strong>where f=distance run in feet and t is time in seconds.</p>
<form id="form">
  Distance in Feet(f):<input id="inputFeet" type="text" name="feet" required><br> Number of Seconds(t):<input id="inputSeconds" type="text" name="seconds" required><br><br>
  <input type="button" onclick="calculateMph()" value="Calculate" />
</form>
<p><b>Your speed in MPH:</b><br>
  <span id="result"></span>
</p>
ild flue
  • 1,323
  • 8
  • 9
0

you didn't specify to where to print the result and document.write result by clearing page

function calculateMph() {
  var feet = document.getElementById("inputFeet").value; //distance in feet
  var time = document.getElementById("inputSeconds").value; //speed in seconds
  var mph = (45 * feet) / (22 * time); //the equation here

  document.getElementById("result").innerHTML= mph ;
}
<h1>Lab: Chapter Two</h1>
<h3>Fill in the form to determine your speed in miles per hour for a particular race that you ran:</h3>
<p><strong>MPH=(15f/22t) <br></strong>where f=distance run in feet and t is time in seconds.</p>
<form id="form">
  Distance in Feet(f):<input id="inputFeet" type="text" name="feet" required><br> Number of Seconds(t):<input id="inputSeconds" type="text" name="seconds" required><br><br>
  <input type="button" onclick="calculateMph()" value="Calculate" />
</form>
<p><b>Your speed in MPH:</b><br>
  <span id="result"></span>
</p>
jasinth premkumar
  • 1,430
  • 1
  • 12
  • 22