By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,747 Members | 1,808 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,747 IT Pros & Developers. It's quick & easy.

Temperature Converter

P: 25
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
Share this Question
Share on Google+
16 Replies


P: 12
Use Math object. It has round, floor and maybe other methods you can use.
Jun 16 '07 #2

Expert 100+
P: 303
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

P: 25
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
Expert Mod 5K+
P: 5,390
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
Expert Mod 5K+
P: 5,390
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
Expert Mod 5K+
P: 5,390
;) note the typo in the above example : params has to be param in the condition ... sorry ;))
Jun 17 '07 #7

P: 25
;) 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
Expert Mod 5K+
P: 5,390
;) 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

P: 25
;) 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
Expert Mod 5K+
P: 5,390
... ;) 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

P: 25
... ;) 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
Expert Mod 5K+
P: 5,390
;) 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

P: 25
;) 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
Expert Mod 5K+
P: 5,390
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

P: 25
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

P: 1
Hey BB!
Your questions have helped me so much, you the bomb!
Where are you from?
Jun 23 '07 #17

Post your reply

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