Temperature Converter

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Brigitte Behrmann
    New Member
    • Jun 2007
    • 25

    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_tem p - 32) * .55
    The only tip/clue I have is to use
    var tempInCelcius = (document.Conve rter.fahrenheit .value - 32) * .55;
    var tempInFahrenhei t = (document.Conve rter.celcius.va lue * 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>Conver t Temperature</title>

    <script language="JavaS cript">

    <!--Hide from old browsers
    function tempConv() {
    var TempInCelcius = document.Conver ter.fahrenheit. value - 32) * .55;
    var TempInFahrenhei t = document.Conver ter.celcius.val ue * 1.8) + 32;

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


    //-->
    </script>

    </head>
    <body>

    <h1>Convert Temperature</h1>
    <form name=data>
    <p><strong>Ente r 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=tempCon v()>
    <br>
    <p><strong>Ente r the temperature in Celcius</strong></p>
    <input type="text" name="fahrenhei t_in" size="10" align="left">
    <input type="Button" align="left" value="Convert to Fahrenheit" onClick="tempCo nv()">


    </body>
    </html>
  • jsakalos
    New Member
    • Jun 2007
    • 12

    #2
    Use Math object. It has round, floor and maybe other methods you can use.

    Comment

    • improvcornartist
      Recognized Expert Contributor
      • May 2007
      • 303

      #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.

      Code:
      <html>
      <head>
      <title>Convert Temperature</title>
      
      <script language="JavaScript">
      
      <!--Hide from old browsers
      function tempConv() {
      var TempInCelcius = (document.Converter.fahrenheit_in.value - 32) * .55;
      var TempInFahrenheit = (document.Converter.celcius.value * 1.8) + 32;
      
      alert(TempInCelcius);
      alert(TempInFahrenheit);
      }
      //-->
      </script>
      
      </head>
      <body>
      
      <h1>Convert Temperature</h1>
      <form name="Converter">
      <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();">
      
      </form>
      </body>
      </html>

      Comment

      • Brigitte Behrmann
        New Member
        • Jun 2007
        • 25

        #4
        Originally posted by improvcornartis t
        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.

        Code:
        <html>
        <head>
        <title>Convert Temperature</title>
        
        <script language="JavaScript">
        
        <!--Hide from old browsers
        function tempConv() {
        var TempInCelcius = (document.Converter.fahrenheit_in.value - 32) * .55;
        var TempInFahrenheit = (document.Converter.celcius.value * 1.8) + 32;
        
        alert(TempInCelcius);
        alert(TempInFahrenheit);
        }
        //-->
        </script>
        
        </head>
        <body>
        
        <h1>Convert Temperature</h1>
        <form name="Converter">
        <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();">
        
        </form>
        </body>
        </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.

        Code:
        <html>
        <head>
        <title>Convert Temperature</title>
         
        <script language="JavaScript">
         
        <!--Hide from old browsers
           function tempConv() {
                var TempInCelcius = (document.Converter.fahrenheit_in.value - 32) * 0.55;
                var TempInFahrenheit = (document.Converter.celcius_in.value * 1.8) + 32;
                window.alert("The temperature you entered as Fahrenheit is equal to " + TempInCelcius + " in Celcius");
                window.alert("The temperature you entered as Celcius is equal to " + TempInFahrenheit + " in Fahrenheit");
        }
        
        //-->
        </script>
         
        </head>
        <body>
         
        <h1>Convert Temperature</h1>
        <form name="Converter">
        <p><strong>Enter the temperature in Fahrenheit</strong></p>
        <input type="text" name="celcius_in" 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();">
         
        </form>
        </body>
        </html>

        Comment

        • gits
          Recognized Expert Moderator Expert
          • May 2007
          • 5388

          #5
          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 ...

          Comment

          • gits
            Recognized Expert Moderator Expert
            • May 2007
            • 5388

            #6
            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:

            [CODE=javascript]
            // when calling the function pass param to it

            function(param) {
            if (params == 'whatever') {
            // do this
            } else {
            // do that
            }
            }
            [/CODE]

            kind regards

            Comment

            • gits
              Recognized Expert Moderator Expert
              • May 2007
              • 5388

              #7
              ;) note the typo in the above example : params has to be param in the condition ... sorry ;))

              Comment

              • Brigitte Behrmann
                New Member
                • Jun 2007
                • 25

                #8
                Originally posted by gits
                ;) 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_tem p – 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.400000000000 006. 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)

                Comment

                • gits
                  Recognized Expert Moderator Expert
                  • May 2007
                  • 5388

                  #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?

                  Comment

                  • Brigitte Behrmann
                    New Member
                    • Jun 2007
                    • 25

                    #10
                    Originally posted by gits
                    ;) 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:
                    Code:
                    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
                    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
                    <html>
                    <head>
                    <title>Convert Temperature</title>
                     
                    <script language="JavaScript">
                     
                    <!--Hide from old browsers
                       function tempConv() {
                            var TempInCelcius = (document.Converter.fahrenheit_in.value - 32) * 0.55;
                            var TempInFahrenheit = (document.Converter.celcius_in.value * 1.8) + 32;
                            window.alert("The temperature you entered as Fahrenheit is equal to " + TempInCelcius + " in Celcius");
                            window.alert("The temperature you entered as Celcius is equal to " + TempInFahrenheit + " in Fahrenheit");
                    }
                    
                    //-->
                    </script>
                     
                    </head>
                    <body>
                     
                    <h1>Convert Temperature</h1>
                    <form name="Converter">
                    
                    <p><strong>Enter the temperature in Fahrenheit</strong></p>
                    <input type="text" name="fahrenheit_in" size="10" align="left">
                    <input type="Button" align="left" value="Convert to Fahrenheit" onClick="tempConv();">
                    <br><p><strong>Enter the temperature in Celcius</strong></p>
                    <input type="text" name="celcius_in" size="10" align="left">
                    <input type="Button" align="left" value="Convert to Celcius" onClick="tempConv();">
                     
                    </form>
                    </body>
                    </html>

                    Comment

                    • gits
                      Recognized Expert Moderator Expert
                      • May 2007
                      • 5388

                      #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_con version' so it looks like:

                      Code:
                      onclick="tempConv('fahrenheit_conversion');"
                      the second call gets 'celsius_conver sion' 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_con version' and the other conversion when it gets 'celsius_conver sion' ... ok? try it ... please ;)

                      kind regards ...

                      Comment

                      • Brigitte Behrmann
                        New Member
                        • Jun 2007
                        • 25

                        #12
                        Originally posted by gits
                        ... ;) 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_con version' so it looks like:

                        Code:
                        onclick="tempConv('fahrenheit_conversion');"
                        the second call gets 'celsius_conver sion' 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_con version' and the other conversion when it gets 'celsius_conver sion' ... 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.
                        Code:
                        <html>
                        <head>
                        <title>Convert Temperature</title>
                         
                        <script language="JavaScript">
                         
                        <!--Hide from old browsers
                        
                           
                           function(param) {
                              if (param == 'fahrenheit_conversion') {
                                  //tempConv() = (document.Converter.fahrenheit_in.value);
                              } else {
                                  //tempConv() = (document.Converter.celcius_in.value);
                            }
                        }
                           function tempConv() {
                                var TempInCelcius = (document.Converter.fahrenheit_in.value - 32) * 0.55;
                                var TempInFahrenheit = (document.Converter.celcius_in.value * 1.8) + 32;
                                window.alert("The temperature you entered as Fahrenheit is equal to " + TempInCelcius + " in Celcius");
                                window.alert("The temperature you entered as Celcius is equal to " + TempInFahrenheit + " in Fahrenheit");
                        }
                        
                        //-->
                        </script>
                         
                        </head>
                        <body>
                         
                        <h1>Convert Temperature</h1>
                        <form name="Converter">
                        
                        <p><strong>Enter the temperature in Fahrenheit</strong></p>
                        <input type="text" name="fahrenheit_in" size="10" align="left">
                        <input type="Button" align="left" value="Convert to Celcius" onclick="tempConv('fahrenheit_conversion');"
                        <br><p><strong>Enter the temperature in Celcius</strong></p>
                        <input type="text" name="celcius_in" size="10" align="left">
                        <input type="Button" align="left" value="Convert to Fahrenheit" onclick="tempConv('celcius_conversion');"
                         
                        </form>
                        </body>
                        </html>

                        Comment

                        • gits
                          Recognized Expert Moderator Expert
                          • May 2007
                          • 5388

                          #13
                          ;) you nearly have it ... replace the script area with the following:

                          [CODE=javascript]<script language="JavaS cript">

                          <!--Hide from old browsers
                          function tempConv(param) {
                          if (param == 'fahrenheit_con version') {
                          var TempInCelcius = (document.Conve rter.fahrenheit _in.value - 32) * 0.55;
                          window.alert("T he temperature you entered as Fahrenheit is equal to " + TempInCelcius + " in Celcius");
                          } else {
                          var TempInFahrenhei t = (document.Conve rter.celcius_in .value * 1.8) + 32;
                          window.alert("T he temperature you entered as Celcius is equal to " + TempInFahrenhei t + " in Fahrenheit");
                          }
                          }
                          //-->
                          </script>
                          [/CODE]

                          that will do the job ... don't give up !! ... ;)

                          kind regards

                          Comment

                          • Brigitte Behrmann
                            New Member
                            • Jun 2007
                            • 25

                            #14
                            Originally posted by gits
                            ;) you nearly have it ... replace the script area with the following:

                            [CODE=javascript]<script language="JavaS cript">

                            <!--Hide from old browsers
                            function tempConv(param) {
                            if (param == 'fahrenheit_con version') {
                            var TempInCelcius = (document.Conve rter.fahrenheit _in.value - 32) * 0.55;
                            window.alert("T he temperature you entered as Fahrenheit is equal to " + TempInCelcius + " in Celcius");
                            } else {
                            var TempInFahrenhei t = (document.Conve rter.celcius_in .value * 1.8) + 32;
                            window.alert("T he temperature you entered as Celcius is equal to " + TempInFahrenhei t + " in Fahrenheit");
                            }
                            }
                            //-->
                            </script>
                            [/CODE]

                            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

                            Comment

                            • gits
                              Recognized Expert Moderator Expert
                              • May 2007
                              • 5388

                              #15
                              hey ;)) ...

                              ok last step - after the line:

                              [CODE=javascript]
                              var TempInCelcius = (document.Conve rter.fahrenheit _in.value - 32) * 0.55;
                              [/CODE]

                              you put ... that rounds the value:

                              [CODE=javascript]
                              TempInCelcius = Math.round(Temp InCelcius);
                              [/CODE]

                              some lines later you do the same for TempInFahrenhei t ... all right? :)

                              kind regrads

                              Comment

                              Working...