Click to See Complete Forum and Search --> : fahrenheit conversion & celsius conversion


fisk
03-27-2003, 12:28 PM
can anyone tell me what is wrong with the code on this it is giving me the same answers for both entries. Thanks here it is:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Web Programming 1</title>
<script language="JavaScript">
function temperatureConversion()
{
var originalCelsius = 0;
var convertedFahrenheit = 0;
var response = prompt ("Enter a number representing " + " a Celsius temp. I will convert it " + " to its Fahrenheit equivalent and " + " display it in the status bar at the bottom " + " of the screen", "0");
originalCelsius = parseFloat (response);
if (isNaN (originalCelsius))
{
window.defaultStatus = ("Non-numeric value entered!");
}
else
{
convertedFahrenheit = 9 / 5 * (originalCelsius + 32);
window.defaultStatus = (originalCelsius + "degrees Celsius = " + convertedFahrenheit + " degrees Fahrenheit");
}
}
function temperatureConversion()
{

var originalFahrenheit = 0;
var convertedCelsius = 0;

var response = prompt ("Enter a number representing " + " a Fahrenheit temp. I will convert it " + " to its Celsius equivalent and " + " display it in the status bar at the bottom " + " of the screen", "0");
originalFahrenheit = parseFloat (response);
if (isNaN (originalFahrenheit))
{
window.defaultStatus = ("Non-numeric value entered!");
}
else
{
convertedCelsius =5 / 9 * (originalFahrenheit - 32);
window.defaultStatus = (originalFahrenheit + "degrees Fahrenheit = " + convertedCelsius + " degrees Celsius");
}
}
</script>
</head>

<pre>
<input type = "button" name = "temp"
value = "Convert Fahrenheit Temp to Celsius Equivalent"
onClick = "temperatureConversion()">
</pre>

<body bgcolor="white">
<form name="myform">

<pre>
<input type = "button" name = "temp"
value = "Convert Celsius Temp to Fahrenheit Equivalent"
onClick = "temperatureConversion()">

</pre>
</form>

</body>

</html>

havik
03-27-2003, 01:02 PM
The problem is that the function: temperatureConversion()
is defined twice. Try change the name of these functions. I tryed celtofar() and fartocel() and it worked.

Here it is, that's all that needed to be changed

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Web Programming 1</title>
<script language="JavaScript">

function celtofar()
{
var originalCelsius = 0;
var convertedFahrenheit = 0;
var response = prompt ("Enter a number representing " + " a Celsius temp. I will convert it " + " to its Fahrenheit equivalent and " + " display it in the status bar at the bottom " + " of the screen", "0");
originalCelsius = parseFloat (response);
if (isNaN (originalCelsius))
{
window.defaultStatus = ("Non-numeric value entered!");
}
else
{
convertedFahrenheit = 9 / 5 * (originalCelsius + 32);
window.defaultStatus = (originalCelsius + "degrees Celsius = " + convertedFahrenheit + " degrees Fahrenheit");
}
}
function fartocel()
{

var originalFahrenheit = 0;
var convertedCelsius = 0;

var response = prompt ("Enter a number representing " + " a Fahrenheit temp. I will convert it " + " to its Celsius equivalent and " + " display it in the status bar at the bottom " + " of the screen", "0");
originalFahrenheit = parseFloat (response);
if (isNaN (originalFahrenheit))
{
window.defaultStatus = ("Non-numeric value entered!");
}
else
{
convertedCelsius =5 / 9 * (originalFahrenheit - 32);
window.defaultStatus = (originalFahrenheit + "degrees Fahrenheit = " + convertedCelsius + " degrees Celsius");
}
}
</script>
</head>

<pre>
<input type = "button" name = "temp"
value = "Convert Fahrenheit Temp to Celsius Equivalent"
onClick = "fartocel()">
</pre>

<body bgcolor="white">
<form name="myform">

<pre>
<input type = "button" name = "temp"
value = "Convert Celsius Temp to Fahrenheit Equivalent"
onClick = "celtofar()">

</pre>
</form>

</body>

</html>