Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Reply

Join Date: Apr 2006
Posts: 6
Reputation: viet_mafia is an unknown quantity at this point 
Solved Threads: 0
viet_mafia viet_mafia is offline Offline
Newbie Poster

Javascript help!

 
0
  #1
Mar 20th, 2007
Hi everyone, I'm pretty new to Javascript and I guess I'm pretty nooby at it so I have a problem with some Javascript I'm trying to write. Basically I'm creating some Javascript that will allow me to calculate someone's Body Mass Index (BMI) and so far with my code have absolutely no idea what's wrong with it. If you guys could lead me in the right direction it would be much appreciated :cheesy:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  3. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  4.  
  5. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  6.  
  7. <head><title>BMI Calculator</title>
  8.  
  9. <script type="javascript">
  10. <!-- Hide script from non-script-friendly browsers
  11. function BMI Calculator(bmi)
  12. {
  13. var bmi = w / (h*h);
  14. return bmi;
  15. }
  16. //stop hiding the script-->
  17. </script>
  18. </head>
  19.  
  20. <body>
  21. <script type="javascript">
  22. <!-- Hide script from non-script-friendly browsers
  23. var n;
  24. var a;
  25. var h;
  26. var w;
  27. var bmi;
  28. n = prompt("What is your age? ","");
  29. if (n <= 17)
  30. {
  31. document.write("We are unable to calculate your BMI");
  32. }
  33. else if(age => 18)
  34. {
  35. h = prompt("What is your height in metres?","");
  36. w = prompt("What is your weight in kilograms?","");
  37. bmi = w / (h*h);
  38.  
  39. if (bmi <= 19)
  40. {
  41. message = "Underweight";
  42. }
  43. else if (bmi >= 20 && bmi <= 25)
  44. {
  45. message = "Acceptable";
  46. }
  47. else if (bmi >= 26 && <=30)
  48. {
  49. message = "Overweight";
  50. }
  51. else if (bmi >= 31)
  52. {
  53. message = "Obese";
  54. }
  55. else
  56. {
  57. message = "Error!";
  58. }
  59.  
  60. content = ("<html><head><title>Your BMI</title></head><body><h1>BODY MASS INDEX (BMI) CALCULATOR</h1>
  61. <p>Your height= " + h + "<br>Your weight= " + w + <br>Your BMI= " + bmi + "</p>
  62. <p><h2>what your BMI Indicates</h2><br>" + message + ";
  63.  
  64. document.write(content);
  65. }
  66. // stop hiding the script-->
  67. </script>
  68. </body>
  69. </html>
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 121
Reputation: manal is an unknown quantity at this point 
Solved Threads: 17
manal's Avatar
manal manal is offline Offline
Junior Poster

Re: Javascript help!

 
0
  #2
Mar 20th, 2007
Originally Posted by
[code
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head><title>BMI Calculator</title>

<script type="text/javascript">
<!-- Hide script from non-script-friendly browsers
function BMI Calculator(bmi)
{
var bmi = w / (h*h);
return bmi;
}
//stop hiding the script-->
</script>
</head>

<body>
<script type="text/javascript">
<!-- Hide script from non-script-friendly browsers
var n;
var a;
var h;
var w;
var bmi;
n = prompt("What is your age? ","");
if (n <= 17)
{
document.write("We are unable to calculate your BMI");
}
else if(n => 18)
{
h = prompt("What is your height in metres?","");
w = prompt("What is your weight in kilograms?","");
bmi = w / (h*h);

if (bmi <= 19)
{
message = "Underweight";
}
else if (bmi >= 20 && bmi <= 25)
{
message = "Acceptable";
}
else if (bmi >= 26 && bmi <=30)
{
message = "Overweight";
}
else if (bmi >= 31)
{
message = "Obese";
}
else
{
message = "Error!";
}

content = ("<html><head><title>Your BMI</title></head><body><h1>BODY MASS INDEX (BMI) CALCULATOR</h1>
<p>Your height= " + h + "<br>Your weight= " + w + "<br>Your BMI= " + bmi + "</p>
<p><h2>what your BMI Indicates</h2><br>" + message + "</p>");

document.write(content);
}
// stop hiding the script-->
</script>
</body>
</html>[/code]
it had simple mistakes , after i correct it , it works with me and my bmi was acceptable :cheesy:
"give only what u willing to receive "
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 3,203
Reputation: MidiMagic has a spectacular aura about MidiMagic has a spectacular aura about 
Solved Threads: 164
MidiMagic's Avatar
MidiMagic MidiMagic is offline Offline
Nearly a Senior Poster

Re: Javascript help!

 
0
  #3
Mar 20th, 2007
Your scripts go in the head, not the body.
Daylight-saving time uses more gasoline
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 6
Reputation: viet_mafia is an unknown quantity at this point 
Solved Threads: 0
viet_mafia viet_mafia is offline Offline
Newbie Poster

Re: Javascript help!

 
0
  #4
Mar 21st, 2007
Hey guys, i followed your suggestions but it still doesnt work for me. I don't get any prompts or anything. I've done the right thing and saved it as javascript.js haven't I? or should i save it as something else? thanks for the help so far.

What i have so far:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  3. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  4.  
  5. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  6.  
  7. <head><title>BMI Calculator</title>
  8.  
  9. <script type="text/javascript">
  10. <!-- Hide script from non-script-friendly browsers
  11. function BMI Calculator(bmi)
  12. {
  13. var bmi = w / (h*h);
  14. return bmi;
  15. }
  16. //stop hiding the script-->
  17. </script>
  18.  
  19. <script type="text/javascript">
  20. <!-- Hide script from non-script-friendly browsers
  21. var n;
  22. var a;
  23. var h;
  24. var w;
  25. var bmi;
  26. n = prompt("What is your age? ","");
  27. if (n <= 17)
  28. {
  29. document.write("We are unable to calculate your BMI");
  30. }
  31. else if(n => 18)
  32. {
  33. h = prompt("What is your height in metres?","");
  34. w = prompt("What is your weight in kilograms?","");
  35. bmi = w / (h*h);
  36.  
  37. if (bmi <= 19)
  38. {
  39. message = "Underweight";
  40. }
  41. else if (bmi >= 20 && bmi <= 25)
  42. {
  43. message = "Acceptable";
  44. }
  45. else if (bmi >= 26 && bmi <=30)
  46. {
  47. message = "Overweight";
  48. }
  49. else if (bmi >= 31)
  50. {
  51. message = "Obese";
  52. }
  53. else
  54. {
  55. message = "Error!";
  56. }
  57. }
  58. // stop hiding the script-->
  59. </script>
  60. </head>
  61.  
  62. <body>
  63.  
  64. comment= ("<html><head><title>Your BMI</title></head><body><h1>BODY MASS INDEX (BMI) CALCULATOR</h1>
  65. <p>Your height= " + h + "<br>Your weight= " + w + "<br>Your BMI= " + bmi + "</p>
  66. <p><h2>what your BMI Indicates</h2><br>" + message + "</p>");
  67.  
  68. document.write (comment);
  69. </body>
  70. </html>
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 1,091
Reputation: MattEvans is a jewel in the rough MattEvans is a jewel in the rough MattEvans is a jewel in the rough 
Solved Threads: 63
Moderator
Featured Poster
MattEvans's Avatar
MattEvans MattEvans is offline Offline
Veteran Poster

Re: Javascript help!

 
0
  #5
Mar 21st, 2007
it's permissable to write <script> in the body section; be aware that the script will run at weird times though ( as the script element is parsed ). the only way the script you have got WILL work is if part of that script runs as the body is parsed; because you use document.write( ); which is parse/position dependant ( i.e. you NEED the script to run as the script element within the body is parsed )

be aware; document.write( ) doesn't work in browsers like Firefox - when they goes into 'true' XML parsing mode. That XHTML DOCTYPE might send Firefox into XML mode... in which case; Firefox will do unpredictable things with document.write ( i.e. nothing ):

http://www.mozilla.org/docs/web-deve...html#xhtmldiff

I would say you'll be alright; since I doubt you're sending your XHTML page with the application/xhtml+xml content type header - but regardless! document.write( ) isn't 'permitted' in XHTML:

http://www.w3.org/MarkUp/2004/xhtml-faq#docwrite

try the code that manal posted; but stick a nice HTML4.0 transitional Doctype on top; instead of that XHTML1.0 strict one
Last edited by MattEvans; Mar 21st, 2007 at 5:13 am.
Plato forgot the nullahedron..
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 6
Reputation: viet_mafia is an unknown quantity at this point 
Solved Threads: 0
viet_mafia viet_mafia is offline Offline
Newbie Poster

Re: Javascript help!

 
0
  #6
Mar 21st, 2007
Thanks for the tips Matt, however I'm restricted to using only XHTML so I don't really have a choice. I wrote up another script but when i click on calculate it does nothing and just shows theres an error on the page. Could anyone help please?

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  3. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  4.  
  5. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  6.  
  7. <head><title>BMI Calculator</title>
  8.  
  9. <script type="text/javascript">
  10. <!-- Hide script from non-script-friendly browsers
  11. function BMI_Calculator()
  12. {
  13. n = prompt('What is your age?',"");
  14.  
  15. if (n <= 17)
  16. { document.write("We are unable to calculate your BMI"); }
  17. else if(n => 18)
  18. { h = prompt('What is your height in metres?',"");
  19. w = prompt('What is your weight in kilograms?',"");
  20. bmi = Math.round(w / (h*h));
  21.  
  22. switch(bmi)
  23. {
  24. case bmi <= 19:
  25. message = "Underweight";
  26. break;
  27. case bmi >= 20 && bmi <= 25:
  28. message = "Acceptable";
  29. break;
  30. case bmi >= 26 && bmi <=30:
  31. message = "Overweight";
  32. break;
  33. case bmi >= 31:
  34. message = "Obese";
  35. break;
  36. default: message = "Error!";
  37. break;
  38. }
  39.  
  40. comment = "<html><head><title>Your BMI</title></head><body>" +
  41. "<h1>BODY MASS INDEX (BMI) CALCULATOR</h1><p>Your height = " +
  42. h + "<br>Your weight = " + w + "<br>Your BMI = " + bmi + "</p><p>" +
  43. "<h2>what your BMI Indicates</h2><br>" + message + "</p>";
  44.  
  45. document.write (comment);
  46.  
  47. }
  48. // stop hiding the script-->
  49. </script>
  50. </head>
  51.  
  52. <body>
  53.  
  54. <a href="javascript: BMI_Calculator()">Calculate</a>
  55.  
  56. </body>
  57. </html>
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 83
Reputation: rgtaylor is an unknown quantity at this point 
Solved Threads: 2
rgtaylor rgtaylor is offline Offline
Junior Poster in Training

Re: Javascript help!

 
0
  #7
Mar 23rd, 2007
You have 2 scripts, 1 in the head and 1 in the body, but the 1 in the body is writing a new html, head, body etc. tags... so you have a broken html page for the browser when you are done....

In effect you have this structure
html
head
script
/script
/head
body
script
html
head
body
/script
/body
/html

If that doesn't make people go "hun?" as well as your browser, especially when it is set to "strict" I don't know what will....

Computers are the smartest thing in the world... they do only and exactly what we tell them, nothing more, nothing less. They show us our own mistakes in bright vibrant colors and sometimes publish them for the world to see. ALL errors are user errors, computers don't make mistakes, people do.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the JavaScript / DHTML / AJAX Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC