I am trying to add a function to this code for conversion to Kelvin, it seems as though I can't do it. The Celsius to Fahrenheit works fine, i thought I'd start simple just specify the Fahrenheit to Kelvin.
function convert(degree) {
var x;
if (degree == "C") {
x = document.getElementById("c").value * 9 / 5 + 32;
document.getElementById("f").value = Math.round(x);
} else {
x = (document.getElementById("f").value - 32) * 5 / 9;
document.getElementById("c").value = Math.round(x);
} else if {
x = (document.getElementById("f").value - 32) * 5 / 9 + 273.15;
document.getElementById("k").value = Math.round(x);
}
}
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Celcius to Fahrenhet</h2>
<p>Insert a number into one of the input fields below:</p>
<p><input id="c" onkeyup="convert('C')"> degrees Celsius</p>
<p><input id="f" onkeyup="convert('F')"> degrees Fahrenheit</p>
<p><input id="k" onkeyup="convert('K')"> degrees Kelvin</p>
<p>Note that the <b>Math.round()</b> method is used, so that the result will be returned as an integer.</p>
</body>
</html>
Aesthetically everything is the same but function is lost when i add 'else' for Kelvin
Aucun commentaire:
Enregistrer un commentaire