Connecting Tech Pros Worldwide Forums | Help | Site Map

loops

Member
 
Join Date: Jul 2007
Posts: 47
#1: Jul 25 '07
can anyone tell me where to beging with this one?


Write a program in Javascript which uses a while loop to output a conversion table for 1-100 degrees C to degrees F.



Where F= (C* 9/5) + 32

Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,161
#2: Jul 25 '07

re: loops


This sounds like a homework question, and also not a very difficult one.
Look up to the FOR loop in javascript on a tutorial site and you should find all you need to know
epots9's Avatar
Moderator
 
Join Date: May 2007
Location: Canada
Posts: 1,313
#3: Jul 25 '07

re: loops


Quote:

Originally Posted by ballygowanboy

can anyone tell me where to beging with this one?


Write a program in Javascript which uses a while loop to output a conversion table for 1-100 degrees C to degrees F.



Where F= (C* 9/5) + 32

y not use a forloop?

good luck
pbmods's Avatar
Site Moderator
 
Join Date: Apr 2007
Location: Texas
Posts: 5,435
#4: Jul 25 '07

re: loops


Quote:

Originally Posted by epots9

y not use a forloop?

Never ask a question about homework. There is no good response, except for, "I want you to do it this way."

While loops better teach the fundamentals behind looping. When you think about it, a for loop is really just an efficient way of coding a while loop with a counter.

[EDIT: Ok, who did that? You know who you are.]
Member
 
Join Date: Jul 2007
Posts: 47
#5: Jul 26 '07

re: loops


i wrote a simple php script, which i then had to turn into a javascript, it's not working.

i'm unsure when to put "var" in front of a variable in javascript.

can someone tell me what i'm doing wrong, thanks.



Expand|Select|Wrap|Line Numbers
  1. <script language="javascript1.2">
  2.  
  3. document.write("<b>Celcius to Farenheit</b><br>");
  4.  
  5. function getF(x)
  6.     {
  7.     var convert = x * 1.8 + 32;
  8.     return convert;
  9.     }
  10.  
  11.     var i=0;
  12.  
  13.     while(i<=100) {
  14.         result=getF(i);
  15.         document.write "i - result <br>";
  16.         i=i+1;
  17.     }
  18.  
  19. </script>
  20.  
iam_clint's Avatar
Forum Leader
 
Join Date: Jul 2006
Location: Oklahoma
Posts: 1,076
#6: Jul 26 '07

re: loops


this is homework isn't it????

Expand|Select|Wrap|Line Numbers
  1. document.write "i - result <br>";
You had this line mostly correct, although its going to literally write i - result then a break

it needs to look something like this
Expand|Select|Wrap|Line Numbers
  1. document.write(i - result+"<br>");
epots9's Avatar
Moderator
 
Join Date: May 2007
Location: Canada
Posts: 1,313
#7: Jul 26 '07

re: loops


Quote:

Originally Posted by ballygowanboy

i wrote a simple php script, which i then had to turn into a javascript, it's not working.

i'm unsure when to put "var" in front of a variable in javascript.

can someone tell me what i'm doing wrong, thanks.



Expand|Select|Wrap|Line Numbers
  1.  
  2. <script language="javascript1.2">
  3.  
  4. document.write("<b>Celcius to Farenheit</b><br>");
  5.  
  6. function getF(x)
  7.     {
  8.     var convert = x * 1.8 + 32;
  9.     return convert;
  10.     }
  11.  
  12.     var i=0;
  13.  
  14.     while(i<=100) {
  15.         result=getF(i);
  16.         document.write "i - result <br>";
  17.         i=i+1;
  18.     }
  19.  
  20. </script>

without testing everything it looks good, but instead of
[html]
<script language="javascript1.2"></script>
[/html]
use
[html]
<script type="text/javascript"></script>
[/html]
info about script tag

and u should use
Expand|Select|Wrap|Line Numbers
  1. var result=getF(i);
  2. document.write(i - result+"<br>");
  3.  
test that out...

good luck
Member
 
Join Date: Jul 2007
Posts: 47
#8: Jul 26 '07

re: loops


ok, almost working, but it's not writing the value of i

i'm just geting - result

-32
-32.8
-33.6
-34.4.................



Expand|Select|Wrap|Line Numbers
  1. <script language="javascript1.2">
  2.  
  3. document.write("<b>Celcius to Farenheit</b><br>");
  4.  
  5. function getF(x)
  6.     {
  7.     var convert = x * 1.8 + 32;
  8.     return convert;
  9.     }
  10.  
  11.     var i=0;
  12.  
  13.     while(i<=100) {
  14.         var result=getF(i);
  15.         document.write(i - result+"<br>");
  16.         i=i+1;
  17.     }
  18.  
  19. </script>
gits's Avatar
Moderator
 
Join Date: May 2007
Location: Munich, Germany
Posts: 4,136
#9: Jul 26 '07

re: loops


hi ballygowanboy,

please use the correct CODE tags when posting code ... and don't use textformating tags for it ... have a look here

kind regards
pbmods's Avatar
Site Moderator
 
Join Date: Apr 2007
Location: Texas
Posts: 5,435
#10: Jul 26 '07

re: loops


Heya, ballygowanboy.

Check out this article.
Reply


Similar JavaScript / Ajax / DHTML bytes