473,320 Members | 2,158 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

JavaScript syntax problems with simple program

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
9 1663
Markus
6,050 Expert 4TB
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
Thanks so much for replying...I've just tried that but still no joy?
Jan 17 '08 #3
Markus
6,050 Expert 4TB
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
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
6,050 Expert 4TB
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
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
6,050 Expert 4TB
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
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
16,027 Expert Mod 8TB
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

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

Similar topics

699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
54
by: tshad | last post by:
I have a function: function SalaryDisplay(me) { var salaryMinLabel = document.getElementById("SalaryMin"); salaryMinLabel.value = 200; alert("after setting salaryMinLabel = " +...
3
by: COHENMARVIN | last post by:
I have a problem that sounds simple but seems impossible with asp.net. I calculate a variable in the Page_Load event, call it X. Then I want my range validators to fire when a textbox value is...
11
by: =?iso-8859-1?q?Martin_M=FCcke?= | last post by:
Hi, I am looking for a good javascript obfuscator - I found several on the web, but I am looking for one that can also handle javascript in jsp, html and java files - so I need a javascript...
43
by: Bill H | last post by:
25 years ago every computer came with some form of Basic interpreter so you could use yoru computer without having to buy more software. Is Javascript (teamed with HTML) set to become the new...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.