# Thread: Celsius to Fahrenheit

1. Registered User
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.

2. 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. Registered User+
Join Date
Jan 2005
Posts
3,067
I usually don't help people with assignments, but I am bored tonight so:
Code:
```<html>
<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>
<div id="content">
</div>
</body>
</html>```
Last edited by konithomimo; 12-05-2005 at 07:28 PM.

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

5. i think its Math.Round() but not sure - try it and Math.Floor() Math.Celing - how about you search for it

6. Registered User+
Join Date
Jan 2005
Posts
3,067
Here is a better way (it has the loop that you need):
Code:
```<html>
<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>
<div id="content">
</div>
</body>
</html>```

7. Registered User
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. Registered User+
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:

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

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