简体   繁体   中英

I am trying to convert Fahrenheit to Celsius but it's not working what am i doing wrong? - Javascript (needs to have a form and a clear button)

<html>
  <head>
    <title>
    Form
     </title>
  </head>
  <body>
      <script type="text/javascript">
      function calculate (form)
  {
   cel = fah * 0.5555 - 32;
   document.getElementById("finish").innerHTML = cel;
  }

</script>

<form name="myform" action="" method="get"> Turn Fahrenheit to Celsius! <br>
<input type="number" name="fah">
<input type="button" name="button" value="calculate" onClick="calculate(this.form)">
<button type="reset" value="Reset">Reset</button>
</form>
<p id="finish">°C</p>
  </body>
</html>

Edit1: Moved the inner.HTML into the Function

So the reset button is the only thing that works. Is it possible to calculate the math this way?

Adeno Fixed it by declaring what fah was. you can also see your errors with f12. https://stackoverflow.com/users/965051/adeneo

<html>
 <head>
    <title>
        Form
    </title>
</head>
<body>
    <script type="text/javascript">
        function calculate(form) {
            var cel = (document.getElementById("fah").value -32) * 5 / 9;
            document.getElementById("finish").innerHTML = cel;
        }
    </script>
    <form name="myform" action="" method="get"> Turn Fahrenheit to Celsius!
        <br>
        <input type="number" name="fah" id="fah">
        <input type="button" name="button" value="calculate" onClick="calculate(this.form)">
        <button type="reset" value="Reset">Reset</button>
    </form>
    <p id="finish">°C</p>
</body>

You never get the value from the input type = "number"

Try this

<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<script>

    function calculate()
  {
  var fah = document.getElementById('fah').value;

   var cel = parseFloat(fah * 0.5555 - 32);
    document.getElementById("finish").innerHTML = cel;
  }
    </script>
</head>

<body>








<form>
Turn Fahrenheit to Celsius! <br>
<input type="text" name="fah" id="fah">
<input type="button" name="button" value="calculate" onClick="calculate()">
<button type="reset" value="Reset">Reset</button>
</form>
<p id="finish">°C</p>
</body>
</html>

Couple things: You need to set the innerhtml from within the function because the variable is in the function. Or you could have declared the variable outside the function first like var fah = "" then the function. But since you declared it in the function only the function can see it. So i moved the innerhtml set into the function.

Also, javascript likes to use id's not name = "fah" You can call an element by name but id is easier.

i rounded it to integer. you would get 9 decimals your way.

Lastly, innerhtml set clears all the html so you would lose the °C the way you had it.

<html>
  <head>
    <title>
Form
 </title>
 </head>
<body>
    <script type="text/javascript">

    function calculate (form)
{

     var fah = this.fah.value;
   cel = Math.round((fah-32) / 1.8);

     document.getElementById("finish").innerHTML = cel+"°C";
}

</script>

<form name="myform" action="" method="get"> Turn Fahrenheit to Celsius! <br>
<input type="number" name="fah" id = "fah">
<input type="button" name="button" value="calculate"                 onClick="calculate(this.form)">
<button type="reset" value="Reset">Reset</button>
</form>
<p id="finish"></p>
  </body>
</html>

You asked a question on how to create a pizza form a while ago and you deleted it soon as it was down voted a few times.

The point to note here is, it is okay if a few people dislike your question. You've joined StackExchange not to gain points but to learn a few things. Your question could be helpful to a lot of others out there in this world. So here it is the answer to your pizza question

<html>
<body>

<p>A pizza is 13 dollars with no toppings.</p>

<form action="form_action.asp">
<input type="checkbox" name="pizza" value="Pepperoni" id="pep">Pepperoni + 5$<br>
<input type="checkbox" name="pizza" value="Cheese" id="che">Cheese + 4$<br>
<br>
<input type="button" onclick="myFunction()" value="Send order">
<input type="button" onclick="cost()" value="Get cost">
<br><br>
 <input type="text" id="order" size="50"> 
 </form>

<script>
function myFunction() {
var pizza = document.forms[0];
var txt = "";
var i;
for (i = 0; i < pizza.length; i++) {
    if (pizza[i].checked) {
        txt = txt + pizza[i].value + " ";
    }
}
document.getElementById("order").value = "You ordered a pizza with: " + txt;
}

function cost() {
var pep = 5;
var che = 4;
var pizza = 13;
var total = 0;
if (document.getElementById("pep").checked === true) {
  total += pep;
}

if (document.getElementById("che").checked === true) {
  total += che;
}
  document.getElementById("order").value = "The cost is : " + total;
}

</script>

</body>
</html>

Thanks. I hope this helps you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM