I can not get this code to convert a negative Fahrenheit value to a Celsius value using JavaScript. I'm assuming it is a problem with using either floor or isValid, but not sure. My code is successfully converting positive Fahrenheit values to Celsius, but not negative. this is also a program for converting fahrenheit to celsius, miles to kilometers and ounces to grams to clarify
//alert("Input value validated as numeric: " + iv);
var cT = document.getElementsByName("cType");
if (cT[0].checked) {
//to celsius
if (isValid(iv,0)) {
$("result").value = iv + " farenheit = " + FtoC(iv) + " celcius.";
}
}
function FtoC (farenheit) {
var C = (farenheit-32) * (5/9);
return C;
}
function MtoK (miles) {
var K = miles * 1.6;
return K;
}
function OtoG (oz) {
var g = oz * 28.35;
return g;toFixed(3);
}
function isValid(val,floor) {
if (val < floor) {
return false;
}
return true;
}
Here is a super-simple fahrenheit to celsius converter.
const a = document.getElementById("answer"); const b = document.getElementById("butt"); const c = document.getElementById("convert"); var cels = 0; b.addEventListener("click", function() { var fahr = Number(c.value); cels = (1.8*fahr) + 32; a.innerHTML = cels + "<sup>o</sup>C"; })
<input id="convert" type="number" /><sup>o</sup>F <button id="butt">Convert Me!</button> <p id="answer"></p>
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.