www.webdeveloper.com
Results 1 to 9 of 9

Thread: Celsius to Fahrenheit

  1. #1
    Join Date
    Dec 2005
    Posts
    3

    Celsius to Fahrenheit

    I need help writing a javascript.... Here's the instructions....

    Write a script that outputs a celcius to fahrenheit conversion table
    for a range of temperatures from 0 C to 100 C.
    Define a function called C2F
    which takes one argument, the celcius temperature to convert to
    fahrenheit.
    The function should return the fahrenheit
    temperature rounded to the nearest integer value.


    In the body of your document, have a while
    loop (or feel free to use a for loop if you
    prefer) that loops over the celcius values from 0 to 100 and prints
    them out next to the corresponding fahrenheit temperatures as computed
    by your C2F function.


    Please Help... Thanks~~~~!!!!!

  2. #2
    Join Date
    Jan 2004
    Posts
    484
    uhmmm yeah.....
    you should at least try to do it then come here and ask the question especially cause it sounds like you have a class

    so here is a quick example and easy

    <html>
    <BODY>

    <form>
    <input type="text" name="Fahrenheit" value="32" onChange="Celsius.value = 100/(212-32) * (this.value - 32 )">
    <input type="text" name="Celsius" value="0" onChange="Fahrenheit.value = (212-32)/100 * this.value + 32 ">
    </form>


    </BODY>
    </html>

    good luck with your class

  3. #3
    Join Date
    Jan 2005
    Posts
    3,067
    I usually don't help people with assignments, but I am bored tonight so:
    Code:
    <html>
    <head>
    <script type="text/javascript">
    function convert(thisstring)
    {
    document.getElementById('content').innerHTML = '<table border=2><tr><td>Celsius</td><td>Fahrenheit</td></tr>' +thisstring + '</table>';
    }
    
    function create(num,string)
    {
    if(num==101)
    {
    convert(string);
    }
    else
    {
    var f = ((num*(9/5)) + 32).toFixed(2);
    var mystring = string + '<tr><td>' + num + '</td><td>' + f + '</td></tr>';
    num ++;
    create(num,mystring);
    }
    }
    </script>
    </head>
    <body onload="create(1,'')">
    <div id="content">
    </div>
    </body>
    </html>
    Last edited by konithomimo; 12-05-2005 at 08:28 PM.

  4. #4
    Join Date
    Dec 2005
    Posts
    3
    thanks!!! How do I round these up to whole numbers..?

  5. #5
    Join Date
    Apr 2005
    Location
    Bathurst, NSW, Australia
    Posts
    3,357
    i think its Math.Round() but not sure - try it and Math.Floor() Math.Celing - how about you search for it

  6. #6
    Join Date
    Jan 2005
    Posts
    3,067
    Here is a better way (it has the loop that you need):
    Code:
    <html>
    <head>
    <script type="text/javascript">
    function convert(thisstring)
    {
    document.getElementById('content').innerHTML = '<table border=2><tr><td>Celsius</td><td>Fahrenheit</td></tr>' +thisstring + '</table>';
    }
    
    function create()
    {
    var i;
    var f; 
    var mystring = '';
    
    for(i=0;i<101;i++)
    {
    f = Math.round((i*(9/5)) + 32);
    mystring = mystring + '<tr><td>' + i + '</td><td>' + f + '</td></tr>';
    }
    convert(mystring);
    }
    </script>
    </head>
    <body onload="create()">
    <div id="content">
    </div>
    </body>
    </html>
    I already made it round up.

  7. #7
    Join Date
    Dec 2005
    Posts
    3
    Another question.... What is the purpose of the STRING????

    And also what does the BODY ONLOAD function do????

  8. #8
    Join Date
    Jan 2005
    Posts
    3,067
    The string is what creates your table. Normally to create a rwo in a table you have:

    <tr><td></td>/<td</td></tr>

    If you want 5 rows then you have:

    <tr><td></td>/<td></td></tr>
    <tr><td></td>/<td></td></tr>
    <tr><td></td>/<td></td></tr>
    <tr><td></td>/<td></td></tr>
    <tr><td></td>/<td></td></tr>

    which takes up a lot of space, so instead we create each row seperatley but in a loop so that they are all created at one time. If we didnt do it as a string (instead as innerHTML) then the browser would add in </tbody></table> to the end of the first line which would just give you a one row table. Now there are ways around that, which my original post (which i replaced) did. That is more work than needed though.

    Onload tells the browser that once the body of the page begins to load (as soon as the page begins to open) run the function specified. In terms of my code it was:

    onload="create()"

    which means run create() once the page begins to load.
    Last edited by konithomimo; 12-07-2005 at 02:12 PM.

  9. #9
    Join Date
    Oct 2005
    Posts
    79
    . . .
    Last edited by Ancora; 12-10-2005 at 06:13 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles