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

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>

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 08: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 02:12 PM.

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

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
•

"

"

## X vBulletin 4.2.2 Debug Information

• Page Generation 0.15664 seconds
• Memory Usage 2,918KB
• Queries Executed 13 (?)
Template Usage (33):
• (2)bbcode_code
• (1)footer
• (1)forumjump
• (1)forumrules
• (1)gobutton
• (9)memberaction_dropdown
• (1)navbar
• (1)navbar_moderation
• (1)navbar_noticebit
• (1)navbar_tabs
• (2)option
• (9)postbit
• (9)postbit_onlinestatus
• (9)postbit_wrapper
• (1)spacer_close
• (1)spacer_open
• (1)tagbit_wrapper

Phrase Groups Available (6):
• global
• inlinemod
• postbit
• posting
• reputationlevel
Included Files (26):
• ./global.php
• ./includes/class_bootstrap.php
• ./includes/init.php
• ./includes/class_core.php
• ./includes/config.php
• ./includes/functions.php
• ./includes/class_friendly_url.php
• ./includes/class_hook.php
• ./includes/class_bootstrap_framework.php
• ./vb/vb.php
• ./vb/phrase.php
• ./includes/functions_calendar.php
• ./includes/functions_bigthree.php
• ./includes/class_postbit.php
• ./includes/class_bbcode.php
• ./includes/functions_reputation.php
• ./includes/functions_notice.php
• ./packages/vbattach/attach.php
• ./vb/types.php
• ./vb/cache.php
• ./vb/cache/db.php
• ./vb/cache/observer/db.php
• ./vb/cache/observer.php

Hooks Called (70):
• init_startup
• friendlyurl_resolve_class
• init_startup_session_setup_start
• database_pre_fetch_array
• database_post_fetch_array
• init_startup_session_setup_complete
• global_bootstrap_init_start
• global_bootstrap_init_complete
• cache_permissions
• fetch_foruminfo
• global_state_check
• global_bootstrap_complete
• global_start
• style_fetch
• global_setup_complete
• strip_bbcode
• friendlyurl_clean_fragment
• friendlyurl_geturl
• forumjump
• cache_templates
• cache_templates_process
• template_register_var
• template_render_output
• fetch_template_start
• fetch_template_complete
• parse_templates
• notices_check_start
• notices_noticebit
• process_templates_complete
• friendlyurl_redirect_canonical
• bbcode_fetch_tags
• bbcode_create
• postbit_factory
• postbit_display_start
• postbit_imicons
• bbcode_parse_start
• bbcode_parse_complete_precache
• bbcode_parse_complete
• postbit_display_complete
• memberaction_dropdown
• tag_fetchbit_complete
• forumrules
• navbits
• navbits_complete