By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,594 Members | 3,680 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,594 IT Pros & Developers. It's quick & easy.

JavaScript syntax problems with simple program

P: 5
Hello All!

I'm brand new to JavaScript, and i've been set the challenge of writing my first program. It's to classify eggs by their size....I've been sat here for about three hours now and cannot see why it won't run. I'm sure i've made an obvious mistake but i just can't see it.....Can someone kindly help?

[HTML]<HTML>
<HEAD>
<TITLE>
Egg sizes
</TITLE>
<SCRIPT LANGUAGE = "JavaScript">
//A Program for classifying eggs by size

var weight;

weight = window.prompt('Please enter the weight of the egg in grams', '');
weight = parseFloat(weight);
if (weight <53)
{
document.write('This egg is small.')
}
else
{
if (weight >= 53) && (weight <63)
{
document.write('This egg is medium.')
}
...[snipped]...
document.write(That was Eggscellent, Thank you for using this program.'

</SCRIPT>
</HEAD>
<BODY>
</BODY>
</HTML>[/HTML]
Jan 17 '08 #1
Share this Question
Share on Google+
9 Replies


Markus
Expert 5K+
P: 6,050
What defines the weight of the Egg, eggzactly?

I'm sorry! It had to be done!

Forget about the question! I just read a little more..

Try changing:
Expand|Select|Wrap|Line Numbers
  1. script language="javascript"
  2.  
to
Expand|Select|Wrap|Line Numbers
  1. script type="text/javascript"
  2.  
The 'language' attribute is now deprecated.
Jan 17 '08 #2

P: 5
Thanks so much for replying...I've just tried that but still no joy?
Jan 17 '08 #3

Markus
Expert 5K+
P: 6,050
No problem!

After having a glance over your, not so pretty, code, i found some problems!

When you, in an if(else) statement, have something like || / && you need an extra set of parenthesis' for each || // && you use - you'll see what i mean in the revised code.

I also changed it to if.. else if... else statements. I also made the code look nicer.. easier to debug, exactly like in these situations!
Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2. //A Program for classifying eggs by size
  3.  
  4. var weight;
  5.  
  6. weight = window.prompt('Please enter the weight of the egg in grams', '');
  7. weight = parseFloat(weight);
  8. if (weight <53)
  9. {
  10. document.write('This egg is small.')
  11. }
  12.      else if((weight >= 53) && (weight <63))
  13.         {
  14.         document.write('This egg is medium.')
  15.         }
  16. [snipped]
  17. else{
  18.     document.write("Reload.")
  19. }
  20. document.write("That was Eggscellent, Thank you for using this program.")
  21.  
  22. </script>
  23.  
Anything you want answering just ask :)
Jan 17 '08 #4

P: 5
Oh my god..Thank you so much it's amazing!!

One thing though, we have not yet learnt the reload code. What does this do and how can I express this another way..

Thanks again. Was it that really that awful for a first try?!!!
Jan 17 '08 #5

Markus
Expert 5K+
P: 6,050
The document.write("Reload.")?

That's really of no relevance, it's not even a javascript function; it's just good practice to end an IF statement with an ELSE statement.

Don't worry about it.

:)

Post back whenever you need some help!

Please also remember to use the relevant CODE tags. ie. [ CODE=javascript]javascript code goes here...[/code]
Jan 17 '08 #6

P: 5
Thanks so much for helping me!!

Just one final thing (sorry i'm a pain)

Is it possible to end the code without the reload expression. Purely because i need to show this to my tutor, and he knows we haven't learnt it yet!!!

I really appreciate your kindness, I've learnt more in the last hour than I have in the last week!!!!
Jan 17 '08 #7

Markus
Expert 5K+
P: 6,050
Thanks so much for helping me!!

Just one final thing (sorry i'm a pain)

Is it possible to end the code without the reload expression. Purely because i need to show this to my tutor, and he knows we haven't learnt it yet!!!

I really appreciate your kindness, I've learnt more in the last hour than I have in the last week!!!!
There is no 'reload' expression!

It's just a string of text.. it's meaningless; it has no meaning; no javascript-function; just random text!

Haha.

And oh dear... i've just written your homework!
Aslong as you've understood and improved your javascript with this, i can sleep tonight?
Jan 17 '08 #8

P: 5
It's ok you can sleep tonight!!
I'm a mature (well 26 yr old) student taking an evening class....!
Thanks so much for your help , it's definitely making sense now!
Jan 18 '08 #9

acoder
Expert Mod 15k+
P: 16,027
We have quite a strict policy on homework/class assignments - please read that section of the guidelines. I'm removing parts of the code as per the guidelines.

MODERATOR
Jan 18 '08 #10

Post your reply

Sign in to post your reply or Sign up for a free account.