PDA

View Full Version : Help in Javascript please



marylove2007
Mar 12th, 2007, 02:56 PM
I am trying to write these functions , functions to write a script that enables the user to enter either a Fahrenheit or a Celsius temperature and displays the Celsius or Fahrenheit equivalent.

my XHTML document it contain a form two buttons—one to initiate the conversion from Fahrenheit to Celsius and one to initiate the conversion from Celsius to Fahrenheit. All my calculations should be done in floating point, but results should be rounded.

Here is my code I didn’t Know why it did not work


<?xml version = "1.0"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns = "http://www.w3.org/1999/xhtml">

<head>


<title>Temperature Conversion</title>




</head>

<body>

<SCRIPT Language="JavaScript">

function getCelsius () {
//declare variable and initialize
//gets input for temperature in fahrenheit as floating point number
var degF = parseFloat ( document.tempConverter.degrees.value );

//validate input
if (isNaN(degF)) {
window.alert ("Please enter a numeric value for the temperature.");
//clear fields
document.tempConverter.degrees.value = "";
document.tempConverter.degConverted.value = "";
}

if (! isNaN(degF))
//call function to convert temperature & print converted temperature
document.tempConverter.degConverted.value = celsius ( degF );

return;
}


//get degrees in celsius & print conversion
function getFahrenheit () {
//declare variable and initialize
//gets input for temperature in celcius as floating point number
var degC = parseFloat ( document.tempConverter.degrees.value );

//validate input
if (isNaN(degC)) {
window.alert ("Please enter a numeric value for the temperature.");
//clear fields
document.tempConverter.degrees.value = "";
document.tempConverter.degConverted.value = "";
}

if (! isNaN(degC))
//call function to convert temperature & print converted temperature
document.tempConverter.degConverted.value = fahrenheit ( degC );

return;
}


//calculate conversion from fahrenheit to celsius
function celsius (deg) {
conversion = 5/9 * (deg - 32);

//return conversion rounded to nearest tenth
return conversion.toFixed(1);
}


//calculate conversion from celsius to fahrenheit
function fahrenheit (deg) {
conversion = 9/5 * deg + 32;

//return conversion rounded to nearest tenth
return conversion.toFixed(1);
}
</SCRIPT>


</body>


<h1>Temperature Conversion</h1>


<br><form name="data">
<table border = "1" width="60">
<tr>

<td>Fahrenheit:</td>

<td> <input type="text" name="fahrenheit_in" size="15"></td>

<td> <input type="button" name="click2" value="convert to Celsius" onClick="celsius (deg)"></td>
</tr>
<tr>

<td>Celsius:</td>

<td>
<input type="text" name="celsius_in" size="15"></td>


<td> <input type="button" name="click1" value="convert to Fahrenheit" onClick="fahrenheit (deg)"></td>
</tr>




</form>

</table>

</body>

</HTML>

BarrMan
Mar 12th, 2007, 03:45 PM
Now it doesn't display the error:

<?xml version = "1.0"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns = "http://www.w3.org/1999/xhtml">

<head>


<title>Temperature Conversion</title>




</head>

<body>

<SCRIPT Language="JavaScript">

function getCelsius () {
//declare variable and initialize
//gets input for temperature in fahrenheit as floating point number
var degF = parseFloat ( document.tempConverter.degrees.value );

//validate input
if (isNaN(degF)) {
window.alert ("Please enter a numeric value for the temperature.");
//clear fields
document.tempConverter.degrees.value = "";
document.tempConverter.degConverted.value = "";
}

if (! isNaN(degF))
//call function to convert temperature & print converted temperature
document.tempConverter.degConverted.value = celsius ( degF );

return;
}


//get degrees in celsius & print conversion
function getFahrenheit () {
//declare variable and initialize
//gets input for temperature in celcius as floating point number
var degC = parseFloat ( document.tempConverter.degrees.value );

//validate input
if (isNaN(degC)) {
window.alert ("Please enter a numeric value for the temperature.");
//clear fields
document.tempConverter.degrees.value = "";
document.tempConverter.degConverted.value = "";
}

if (! isNaN(degC))
//call function to convert temperature & print converted temperature
document.tempConverter.degConverted.value = fahrenheit ( degC );

return;
}


//calculate conversion from fahrenheit to celsius
function celsius (deg) {
conversion = 5/9 * (deg - 32);

//return conversion rounded to nearest tenth
return conversion.toFixed(1);
}


//calculate conversion from celsius to fahrenheit
function fahrenheit (deg) {
conversion = 9/5 * deg + 32;

//return conversion rounded to nearest tenth
return conversion.toFixed(1);
}
</SCRIPT>


</body>


<h1>Temperature Conversion</h1>


<br><form name="data">
<table border = "1" width="60">
<tr>

<td>Fahrenheit:</td>

<td> <input type="text" name="fahrenheit_in" size="15"></td>

<td> <input type="button" name="click2" value="convert to Celsius" onClick="celsius (document.data.fahrenheit_in.value)"></td>
</tr>
<tr>

<td>Celsius:</td>

<td>
<input type="text" name="celsius_in" size="15"></td>


<td> <input type="button" name="click1" value="convert to Fahrenheit" onClick="fahrenheit (document.data.celsius_in.value)"></td>
</tr>




</form>

</table>

</body>

</HTML>
Your problem was that you called a function and as a parameter value you put in the function's parameter instead of putting a value inside.