I am writing a temperature converter and can't seem to figure out what is wrong with my javascript. I can't even get it to alert "running". My function used to be called convert, but I thought maybe that was a key word of js, so I changed it to tempConvert. The code is supposed to convert temperatures between one another.
Two tests I use are
32F = 0C
and
72F = 22.22222...C
It work fine in prompt and alert messages, now I want to use input boxes.
<!DOCTYPE html />
<html>
<head>
<script type="text/javascript">
function tempConvert(){
alert("running.");
var f = document.getElementById("fahrenheit").value; //Gets the value for farenheit.
var c = document.getElementById("celsius").value; //Gets the value for celsius
var tf = isNumber(f); //Test if the value of f is a number.
var tc = isNumber(c); //Test if the value of c is a number.
if (tf = true){
//if f is a number run this
c = (f-32)/1.8; //conversion from f to c
document.getElementById("celsius").value = c; //sets the value of the celsius input box to c.
alert(c);
} else if (tc = true){
// does the same as previous if statement, switching temperature types.
f = (c+32)*1.8;
document.getElementById("fahrenheit").value = f;
} else {
alert("One of your inputs are invalid.");
// alerts the user if f and c are not a number.
}
}
funcion isNumber(test){ //A custom function(method) used to test if f or c is a number
return !isNaN(parseFloat(test)) && isFinite(test); //copied from another article in stackoverflow (http://stackoverflow.com/questions/6449611/how-to-check-whether-a-value-is-a-number-in-javascript-or-jquery)
}
</script>
</head>
<body>
<input id="fahrenheit" placeholder="fahrenheit"></input> =
<input id="celsius" placeholder="celsius"></input>
<input type="button" value="convert" onclick="tempConvert()" />
</body>
</html>