473,320 Members | 2,147 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Temperature Converter

I am absolutely stuck with this one.
I have to create a temperature conversion calculator that rounds the resulting temperature to the nearest whole number & vice versa. The result must be displayed in a window alert. The given formula is (Fahrenheit_temp - 32) * .55
The only tip/clue I have is to use
var tempInCelcius = (document.Converter.fahrenheit.value - 32) * .55;
var tempInFahrenheit = (document.Converter.celcius.value * 1.8) + 32

I have tried, deleted, tried and deleted again. I am so frustrated and feeling suicidal that I am just not grasping the concept.

PLEASE HELP!

<html>
<head>
<title>Convert Temperature</title>

<script language="JavaScript">

<!--Hide from old browsers
function tempConv() {
var TempInCelcius = document.Converter.fahrenheit.value - 32) * .55;
var TempInFahrenheit = document.Converter.celcius.value * 1.8) + 32;

?????? - I cannot get the calc right????????


//-->
</script>

</head>
<body>

<h1>Convert Temperature</h1>
<form name=data>
<p><strong>Enter the temperature in Fahrenheit</strong></p>
<input type="text" name="Celcius" size="10" align="left">
<input type="Button" align="left" value="Convert to Celcius" onClick=tempConv()>
<br>
<p><strong>Enter the temperature in Celcius</strong></p>
<input type="text" name="fahrenheit_in" size="10" align="left">
<input type="Button" align="left" value="Convert to Fahrenheit" onClick="tempConv()">


</body>
</html>
Jun 16 '07 #1
16 4392
Use Math object. It has round, floor and maybe other methods you can use.
Jun 16 '07 #2
improvcornartist
303 Expert 100+
You had a few syntax errors. Look at the following code for some corrections. If you have more questions while finishing your project, I'll be glad to help.

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <title>Convert Temperature</title>
  4.  
  5. <script language="JavaScript">
  6.  
  7. <!--Hide from old browsers
  8. function tempConv() {
  9. var TempInCelcius = (document.Converter.fahrenheit_in.value - 32) * .55;
  10. var TempInFahrenheit = (document.Converter.celcius.value * 1.8) + 32;
  11.  
  12. alert(TempInCelcius);
  13. alert(TempInFahrenheit);
  14. }
  15. //-->
  16. </script>
  17.  
  18. </head>
  19. <body>
  20.  
  21. <h1>Convert Temperature</h1>
  22. <form name="Converter">
  23. <p><strong>Enter the temperature in Fahrenheit</strong></p>
  24. <input type="text" name="celcius" size="10" align="left">
  25. <input type="Button" align="left" value="Convert to Celcius" onClick="tempConv();">
  26. <br>
  27. <p><strong>Enter the temperature in Celcius</strong></p>
  28. <input type="text" name="fahrenheit_in" size="10" align="left">
  29. <input type="Button" align="left" value="Convert to Fahrenheit" onClick="tempConv();">
  30.  
  31. </form>
  32. </body>
  33. </html>
Jun 17 '07 #3
You had a few syntax errors. Look at the following code for some corrections. If you have more questions while finishing your project, I'll be glad to help.

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <title>Convert Temperature</title>
  4.  
  5. <script language="JavaScript">
  6.  
  7. <!--Hide from old browsers
  8. function tempConv() {
  9. var TempInCelcius = (document.Converter.fahrenheit_in.value - 32) * .55;
  10. var TempInFahrenheit = (document.Converter.celcius.value * 1.8) + 32;
  11.  
  12. alert(TempInCelcius);
  13. alert(TempInFahrenheit);
  14. }
  15. //-->
  16. </script>
  17.  
  18. </head>
  19. <body>
  20.  
  21. <h1>Convert Temperature</h1>
  22. <form name="Converter">
  23. <p><strong>Enter the temperature in Fahrenheit</strong></p>
  24. <input type="text" name="celcius" size="10" align="left">
  25. <input type="Button" align="left" value="Convert to Celcius" onClick="tempConv();">
  26. <br>
  27. <p><strong>Enter the temperature in Celcius</strong></p>
  28. <input type="text" name="fahrenheit_in" size="10" align="left">
  29. <input type="Button" align="left" value="Convert to Fahrenheit" onClick="tempConv();">
  30.  
  31. </form>
  32. </body>
  33. </html>
I have made some changes but I still have the following problems:
1. If I enter 100 in the fahrenheit box and convert to celcius I get a temperature altert of -17.6 but it should return a value of 37.4
2. It does the function twice on click as I have not separated the entry values into either or.... don't know how!
3. I also need to round the returned numbers off to not include decimal points.

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <title>Convert Temperature</title>
  4.  
  5. <script language="JavaScript">
  6.  
  7. <!--Hide from old browsers
  8.    function tempConv() {
  9.         var TempInCelcius = (document.Converter.fahrenheit_in.value - 32) * 0.55;
  10.         var TempInFahrenheit = (document.Converter.celcius_in.value * 1.8) + 32;
  11.         window.alert("The temperature you entered as Fahrenheit is equal to " + TempInCelcius + " in Celcius");
  12.         window.alert("The temperature you entered as Celcius is equal to " + TempInFahrenheit + " in Fahrenheit");
  13. }
  14.  
  15. //-->
  16. </script>
  17.  
  18. </head>
  19. <body>
  20.  
  21. <h1>Convert Temperature</h1>
  22. <form name="Converter">
  23. <p><strong>Enter the temperature in Fahrenheit</strong></p>
  24. <input type="text" name="celcius_in" size="10" align="left">
  25. <input type="Button" align="left" value="Convert to Celcius" onClick="tempConv();">
  26. <br>
  27. <p><strong>Enter the temperature in Celcius</strong></p>
  28. <input type="text" name="fahrenheit_in" size="10" align="left">
  29. <input type="Button" align="left" value="Convert to Fahrenheit" onClick="tempConv();">
  30.  
  31. </form>
  32. </body>
  33. </html>
  34.  
Jun 17 '07 #4
gits
5,390 Expert Mod 4TB
hi ...

have a look at your input-fields ... that have the wrong order! correct it and it should work. I've seen that you know how to round a floating number ... so you should be able to solve this problem ...

kind regards ...
Jun 17 '07 #5
gits
5,390 Expert Mod 4TB
and your problem nr. 2. - you should pass the value of your input-field or at least a param to differ between the buttons within your function ... so that you process only one conversion dependent to your input-parameter ... i give you an example:

Expand|Select|Wrap|Line Numbers
  1. // when calling the function pass param to it
  2.  
  3. function(param) {
  4.     if (params == 'whatever') {
  5.         // do this
  6.     } else {
  7.         // do that
  8.     }
  9. }
  10.  
kind regards
Jun 17 '07 #6
gits
5,390 Expert Mod 4TB
;) note the typo in the above example : params has to be param in the condition ... sorry ;))
Jun 17 '07 #7
;) note the typo in the above example : params has to be param in the condition ... sorry ;))
Okay you have lost me again!!!! Doesn't take much as you may have noticed!
Here is the question, please help me from beginning to end, I just have no idea where what goes!
Question 2 = 15 marks
You convert a Fahrenheit temperature to Celsius using the following formula:
(Fahrenheit_temp – 32) * .55. The result returned from a formula such as this
often results in a number with decimal places. However, you usually display a
temperature rounded to the nearest whole number. For example, when you
convert the Fahrenheit temperature of 100 degrees to Celsius, the result is
47.400000000000006. Create a temperature conversion calculator that rounds
the resulting temperatures to the nearest whole number and vice versa. See
example below.

CONVERT TEMPERATURE
Enter the temperature in Fahrenheit:
(text box) (Button: Convert to Celcius)

Enter the temperature in Celcius:
(text box) (Button: Convert to Fahrenheit)
Jun 17 '07 #8
gits
5,390 Expert Mod 4TB
;) first have a look at your html form. you enter 'celsius_in' but you want 'fahrenheit_in' at this point ... right? switch it ... at first ... have a look at the result ... it should be ok then?
Jun 17 '07 #9
;) first have a look at your html form. you enter 'celsius_in' but you want 'fahrenheit_in' at this point ... right? switch it ... at first ... have a look at the result ... it should be ok then?
Okay, did that but I still get two responses. Gives me the temp in the window alert, click OK and then I get the 2nd alert!
Where does the param code you suggested fit in with what I got:
Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html>
  4. <head>
  5. <title>Convert Temperature</title>
  6.  
  7. <script language="JavaScript">
  8.  
  9. <!--Hide from old browsers
  10.    function tempConv() {
  11.         var TempInCelcius = (document.Converter.fahrenheit_in.value - 32) * 0.55;
  12.         var TempInFahrenheit = (document.Converter.celcius_in.value * 1.8) + 32;
  13.         window.alert("The temperature you entered as Fahrenheit is equal to " + TempInCelcius + " in Celcius");
  14.         window.alert("The temperature you entered as Celcius is equal to " + TempInFahrenheit + " in Fahrenheit");
  15. }
  16.  
  17. //-->
  18. </script>
  19.  
  20. </head>
  21. <body>
  22.  
  23. <h1>Convert Temperature</h1>
  24. <form name="Converter">
  25.  
  26. <p><strong>Enter the temperature in Fahrenheit</strong></p>
  27. <input type="text" name="fahrenheit_in" size="10" align="left">
  28. <input type="Button" align="left" value="Convert to Fahrenheit" onClick="tempConv();">
  29. <br><p><strong>Enter the temperature in Celcius</strong></p>
  30. <input type="text" name="celcius_in" size="10" align="left">
  31. <input type="Button" align="left" value="Convert to Celcius" onClick="tempConv();">
  32.  
  33. </form>
  34. </body>
  35. </html>
Jun 17 '07 #10
gits
5,390 Expert Mod 4TB
... ;) ok ... next step: onclick of your button you call the function tempConv();
... right? call it at your first button with a simple string as param lets say: 'fahrenheit_conversion' so it looks like:

Expand|Select|Wrap|Line Numbers
  1. onclick="tempConv('fahrenheit_conversion');"
the second call gets 'celsius_conversion' ok?

so within the function now you may adapt the example from above ... create an if-statement that makes the conversion from fahrenheit to celsius when param is 'fahrenheit_conversion' and the other conversion when it gets 'celsius_conversion' ... ok? try it ... please ;)

kind regards ...
Jun 17 '07 #11
... ;) ok ... next step: onclick of your button you call the function tempConv();
... right? call it at your first button with a simple string as param lets say: 'fahrenheit_conversion' so it looks like:

Expand|Select|Wrap|Line Numbers
  1. onclick="tempConv('fahrenheit_conversion');"
the second call gets 'celsius_conversion' ok?

so within the function now you may adapt the example from above ... create an if-statement that makes the conversion from fahrenheit to celsius when param is 'fahrenheit_conversion' and the other conversion when it gets 'celsius_conversion' ... ok? try it ... please ;)

kind regards ...
I understand you really are trying to get me to help myself, but please understand that after only one week of self help with a text book, I really have no idea what I am doing.
I still have it all wrong: I think I should just give it up and submit an incorrect assignment and hope that by the end of the year I have learnt enough to pass the exam.
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <title>Convert Temperature</title>
  4.  
  5. <script language="JavaScript">
  6.  
  7. <!--Hide from old browsers
  8.  
  9.  
  10.    function(param) {
  11.       if (param == 'fahrenheit_conversion') {
  12.           //tempConv() = (document.Converter.fahrenheit_in.value);
  13.       } else {
  14.           //tempConv() = (document.Converter.celcius_in.value);
  15.     }
  16. }
  17.    function tempConv() {
  18.         var TempInCelcius = (document.Converter.fahrenheit_in.value - 32) * 0.55;
  19.         var TempInFahrenheit = (document.Converter.celcius_in.value * 1.8) + 32;
  20.         window.alert("The temperature you entered as Fahrenheit is equal to " + TempInCelcius + " in Celcius");
  21.         window.alert("The temperature you entered as Celcius is equal to " + TempInFahrenheit + " in Fahrenheit");
  22. }
  23.  
  24. //-->
  25. </script>
  26.  
  27. </head>
  28. <body>
  29.  
  30. <h1>Convert Temperature</h1>
  31. <form name="Converter">
  32.  
  33. <p><strong>Enter the temperature in Fahrenheit</strong></p>
  34. <input type="text" name="fahrenheit_in" size="10" align="left">
  35. <input type="Button" align="left" value="Convert to Celcius" onclick="tempConv('fahrenheit_conversion');"
  36. <br><p><strong>Enter the temperature in Celcius</strong></p>
  37. <input type="text" name="celcius_in" size="10" align="left">
  38. <input type="Button" align="left" value="Convert to Fahrenheit" onclick="tempConv('celcius_conversion');"
  39.  
  40. </form>
  41. </body>
  42. </html>
Jun 17 '07 #12
gits
5,390 Expert Mod 4TB
;) you nearly have it ... replace the script area with the following:

Expand|Select|Wrap|Line Numbers
  1. <script language="JavaScript">
  2.  
  3. <!--Hide from old browsers
  4. function tempConv(param) {
  5.     if (param == 'fahrenheit_conversion') {
  6.         var TempInCelcius = (document.Converter.fahrenheit_in.value - 32) * 0.55;
  7.         window.alert("The temperature you entered as Fahrenheit is equal to " + TempInCelcius + " in Celcius");
  8.     } else {
  9.         var TempInFahrenheit = (document.Converter.celcius_in.value * 1.8) + 32;
  10.         window.alert("The temperature you entered as Celcius is equal to " + TempInFahrenheit + " in Fahrenheit");
  11.       }
  12. }
  13. //-->
  14. </script>
  15.  
that will do the job ... don't give up !! ... ;)

kind regards
Jun 17 '07 #13
;) you nearly have it ... replace the script area with the following:

Expand|Select|Wrap|Line Numbers
  1. <script language="JavaScript">
  2.  
  3. <!--Hide from old browsers
  4. function tempConv(param) {
  5.     if (param == 'fahrenheit_conversion') {
  6.         var TempInCelcius = (document.Converter.fahrenheit_in.value - 32) * 0.55;
  7.         window.alert("The temperature you entered as Fahrenheit is equal to " + TempInCelcius + " in Celcius");
  8.     } else {
  9.         var TempInFahrenheit = (document.Converter.celcius_in.value * 1.8) + 32;
  10.         window.alert("The temperature you entered as Celcius is equal to " + TempInFahrenheit + " in Fahrenheit");
  11.       }
  12. }
  13. //-->
  14. </script>
  15.  
that will do the job ... don't give up !! ... ;)

kind regards
Thank you sooooooooo much. Last question, how do I now round the resulting temperature. I presume I use Math.round but do not know how this fits in.
Kind regards, BB
Jun 17 '07 #14
gits
5,390 Expert Mod 4TB
hey ;)) ...

ok last step - after the line:

Expand|Select|Wrap|Line Numbers
  1. var TempInCelcius = (document.Converter.fahrenheit_in.value - 32) * 0.55;
  2.  
you put ... that rounds the value:

Expand|Select|Wrap|Line Numbers
  1. TempInCelcius = Math.round(TempInCelcius);
  2.  
some lines later you do the same for TempInFahrenheit ... all right? :)

kind regrads
Jun 17 '07 #15
hey ;)) ...

ok last step - after the line:

Expand|Select|Wrap|Line Numbers
  1. var TempInCelcius = (document.Converter.fahrenheit_in.value - 32) * 0.55;
  2.  
you put ... that rounds the value:

Expand|Select|Wrap|Line Numbers
  1. TempInCelcius = Math.round(TempInCelcius);
  2.  
some lines later you do the same for TempInFahrenheit ... all right? :)

kind regrads
Thanks so much, I see what I did wrong:
I entered
Expand|Select|Wrap|Line Numbers
  1.  var TempInCelcius=Math.round(TempInCelcius); instead of TempInCelcius=Math.round(TempInCelcius);
If you are not too busy can you have a look at a post called Phone Number that I have just submitted....
Cheerio
BB
Jun 17 '07 #16
Hey BB!
Your questions have helped me so much, you the bomb!
Where are you from?
Jun 23 '07 #17

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Derek Ross | last post by:
Hello, Say I have a server that's saving the CPU temperature to 'temperature.js' once a second. The contents of the file is one single line: var temperature = "35.5"; And it changes as...
52
by: piaseckiac | last post by:
I am producing a website on air and need a link to change the entire website from standard to metric for temperature, pressure, miles-kilometers, and volume. Thank you.
1
by: deanfamily11 | last post by:
I'm trying to have this program do a simple temperature conversion from Fahrenheit to Celsius. I have confirmed that the other variable is receiving and calculating the conversion, but it is just...
1
by: sjaak | last post by:
Hi, Im just starting out with C#, I try to make a console application where the user has to fill in temperature in celsius degrees and gets fahrenheit, my code sucks ; can anyone help me out with...
5
by: anthony | last post by:
One the computer I am programmig I could see the CPU temperature in the BIOS, is there a system DLL in VB.NET that I can call to display the temperature in my software? Thanks!
1
by: pollardw | last post by:
i have written a small program to convert Fahrenheit to Celsius and I have a minor problem. Here is the code what do i need to change to get it to work? /* Convert Fahrenheit to Celsius */ ...
25
by: kid joe | last post by:
Hi, Is it normal for the temperature of your CPU to increase as time goes on? I've got an Athlon XP 2100+ and I see that the temp is now at 51C where it used to be around 42 to 44. Anyone...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.