PDA

View Full Version : Temperature conversion problem? parseFloat???


Trout69
06-27-2005, 11:16 PM
Now let me first start saying that i have read the posts that are located at http://www.codingforums.com/showthread.php?t=61565&highlight=celsius and am still stuck.

It seems that the code used to help out is in a slighly different format from that which we are using on the course, so no matter how much i stare at it i can't figure it out.

THE PROBLEM: The program runs fine, the debugger doesn't pick up any errors, but the output always remains at 0. I know this has to be something to do with it picking up the result as a string rather than a number, so i've tried to use parseFloat but to no avail. i have tried 2 locations in the code for this commenting out either/or to see if i can get it to work but i just can't.....i know its something very simple, but i don't get to cover debugging in great detail until my next unit so any help would be appreciated.

I have attached both the function libraries for this program, and the code is below - i have highlighted the two places i have tried parseFloat but left them commented out.



<script
language="JavaScript"
src="number_library.js"
type="text/javascript">
</script>

<script
language="JavaScript"
src="conversion_library.js"
type="text/javascript">
</script>

<script
language="JavaScript"
type="text/javascript">

//
// A FUNCTION THAT TAKES AN INPUT VALUE AND CONVERTS IT TO CELSIUS AFTER
// TESTING IF THE INPUT IS NUMERIC. THIS FUNCTION USES THE PRE-DEFINED
// LIBRARIES number_library.js AND conversion_library.js IF THE INPUT IS
// NOT NUMERIC AN ERROR MESSAGE IS SHOWN.
//
function fahrenheitToCelsius (fahrenheitTemp) {

var inputIsNotNumeric = false; // BOOLEAN VALUE - IS THE INPUT A NON-NUMERIC VALUE - INITIALISED TO FALSE
var celsiusTemp = 0.0; // THE OUTPUT VALUE - INITIALISED TO 0.0
//celsiusTemp = parseFloat(celsiusTemp);


//
// TEST THE INPUT VALUE TO SEE IF IT IS NON-NUMERIC OR NOT
//
inputIsNotNumeric = isNotNumeric (fahrenheitTemp);

//
// IF THE INPUT IS NUMERIC - PROCEED WITH CONVERSION TO CELSIUS
// AND DISPLAYING THE OUTPUT OTHERWISE PRODUCE AN ERROR MESSAGE
//
if (inputIsNotNumeric == false) {

celsiusTemp = roundToTwoPlaces(celsiusTemp);
//celsiusTemp = parseFloat (celsiusTemp);
window.alert("The temperature to two decimal places is " + celsiusTemp + ' degrees Celsius.');
}
else {

window.alert("Your input was not a number.");
}
}
</script>


If you would like the complete HTML document please let me know. Basically the GUI is a basic form with an input text box, a convert button and a reset button. you type the number, click convert and the output is displayed in a window alert.

I just really don't know, can't wait to get onto the debugging part of the course, will make my life easier.....

Thanks in advance

Trout

A1ien51
06-28-2005, 02:59 AM
At no point in that function posted do you ever set celsiusTemp to anything other than 0.0....

Eric

Trout69
06-28-2005, 03:19 AM
Thanks eric, i tought that was covered in the function library in my post. I am very new to this so please excuse me. I have tried all the variables individually and they all work, its just when i try and get themto all work together its goes wrong. My debugger does not return any problems so im suck. Please excuse if i'm being ignorant, i'm a real newby......

Trout

Willy Duitt
06-28-2005, 03:01 PM
This is the same homework assignment as here:
http://www.codingforums.com/showthread.php?t=61849

Please note the examples of nesting several of the library functions together... There really is no need to create any more functions if you only need to call the library functions from a button...

.....Willy