Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-14-2011, 02:01 PM   PM User | #1
Pyrius
New to the CF scene

 
Join Date: Jul 2011
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Pyrius is an unknown quantity at this point
Question I get an error for setting a basic variable?

Whenever I try to compile a script I wrote, I get the error, "class, interface, or enum expected" and it points to a spot in my script that I don't think should create an error... I've tried changing different things in the line it points to, but it doesn't help. Here's are some of the errors:

Code:
Code:
var it = 0;
var udt = new array(10);
	
for(it = 0, it < udt.length, it++){
	switch(it){
		case 0: udt[it] = prompt("Type a name", "");
		case 1: udt[it] = prompt("Type a verb", "");
		case 2: udt[it] = prompt("Type an adverb", "");
		//case 3: udt[it] = prompt("Type a 
		//case 4: udt[it] = prompt("Type a 
		//case 5: udt[it] = prompt("Type a 
		//case 6: udt[it] = prompt("Type a 
		//case 7: udt[it] = prompt("Type a 
		//case 8: udt[it] = prompt("Type a 
		//case 9: udt[it] = prompt("Type a 
		//case 10: udt[it] = prompt("Type a
	}
}
document.write(udt[0] + udt[1] + "away as" + udt[2]);
Errors:
Code:
story.java:1: class, interface, or enum expected
var it = 0;
^
story.java:2: class, interface, or enum expected
var udt = new array(10);
^
story.java:4: class, interface, or enum expected
for(it = 0, it < udt.length, it++){
^
story.java:17: class, interface, or enum expected
        }
        ^
I've looked over the first one and I have no idea why that is an error. If the reason is obvious to you, please be polite. I've only been using javascript for about 2 days.

Last edited by Pyrius; 07-14-2011 at 07:32 PM..
Pyrius is offline   Reply With Quote
Old 07-14-2011, 05:22 PM   PM User | #2
Pyrius
New to the CF scene

 
Join Date: Jul 2011
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Pyrius is an unknown quantity at this point
Someone please help... I can't type anything. I even tried making a "Hello World" program, and it won't work!
Pyrius is offline   Reply With Quote
Old 07-14-2011, 05:47 PM   PM User | #3
Hamza7
New Coder

 
Join Date: Jun 2011
Location: Algeirs,Algeria
Posts: 43
Thanks: 5
Thanked 3 Times in 3 Posts
Hamza7 is an unknown quantity at this point
Please show us the full script, not just the error.
Hamza7 is offline   Reply With Quote
Old 07-14-2011, 06:26 PM   PM User | #4
Pyrius
New to the CF scene

 
Join Date: Jul 2011
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Pyrius is an unknown quantity at this point
Code:
var it = 0;
var udt = new array(10);
	
for(it = 0, it < udt.length, it++){
	switch(it){
		case 0: udt[it] = prompt("Type a name", "");
		case 1: udt[it] = prompt("Type a verb", "");
		case 2: udt[it] = prompt("Type an adverb", "");
		//case 3: udt[it] = prompt("Type a 
		//case 4: udt[it] = prompt("Type a 
		//case 5: udt[it] = prompt("Type a 
		//case 6: udt[it] = prompt("Type a 
		//case 7: udt[it] = prompt("Type a 
		//case 8: udt[it] = prompt("Type a 
		//case 9: udt[it] = prompt("Type a 
		//case 10: udt[it] = prompt("Type a
	}
}
document.write(udt[0] + udt[1] + "away as" + udt[2]);
Sorry about that, I forgot.

EDIT: I added it into my first post.

Last edited by Pyrius; 07-14-2011 at 07:33 PM..
Pyrius is offline   Reply With Quote
Old 07-15-2011, 01:28 AM   PM User | #5
alykins
Senior Coder

 
alykins's Avatar
 
Join Date: Apr 2011
Posts: 1,608
Thanks: 37
Thanked 183 Times in 182 Posts
alykins will become famous soon enough
i think you need to set up the "type" of array you are going to use... idk if it is defaulting to something other than string...
eg
var blah= new Array(int, 10).... i don't really know how to set up arrays in javascript (haven't done this yet) but in every other language i've used i've had to set the "type" of array i was creating eg int, string, bool, etc
reading your error i think this is what is happening... so if i am the compiler and idk what kind of array you want so i make an array of ten ints, now you try to cram 10 strings in me im gunna spaz out... i might be off on this but that's my idea
__________________

I code C hash-tag .Net
Reference: W3C W3CWiki .Net Lib
Validate: html CSS
Debug: Chrome FireFox IE
alykins is offline   Reply With Quote
Old 07-15-2011, 02:10 AM   PM User | #6
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,206
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
You are confusing JAVA and JavaSCRIPT.

*LOOK* at the error message you are getting:

Code:
story.java:1: class, interface, or enum expected
var it = 0;
^
That is a JAVA error.

You can't use the keyword var in Java. You can't use document.write in Java.

MAKE UP YOUR MIND: Are you trying to write a Java program or a JavaScript program???

If it's JavaScript, then you can *NOT* try to send the code through a JAVA compiler.
Old Pedant is online now   Reply With Quote
Old 07-15-2011, 02:11 AM   PM User | #7
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,206
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
And, by the way, you do *NOT* need to or want to "compile" a JavaScript program.
Old Pedant is online now   Reply With Quote
Old 07-15-2011, 02:53 AM   PM User | #8
tracknut
Regular Coder

 
Join Date: Aug 2006
Posts: 891
Thanks: 4
Thanked 205 Times in 204 Posts
tracknut is an unknown quantity at this point
Regardless of what language you're programming in, this code:
Code:
for(it = 0, it < udt.length, it++){
should most likely be:
Code:
for(it = 0; it < udt.length; it++){
Dave
tracknut is offline   Reply With Quote
Reply

Bookmarks

Tags
compile, error, javascript, problem, variable

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:33 PM.


Advertisement
Log in to turn off these ads.