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 03-18-2010, 10:18 PM   PM User | #1
jwishy
New to the CF scene

 
Join Date: Mar 2010
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
jwishy is an unknown quantity at this point
Code Help needed by new JavaScript Student

I hope I am posting this in the right place and not doing anything wrong. I am just lost right now in my Java class, I have these 3 questions left on my homework and I just do not understand them. I tried writing the code in code checkers I tried writing the code in Dreamweaver and opening it in my browser. I have tried all that multiple ways and I cannot get any output. The better answer would be for me to know what the output would be without having to write and execute it but I I don't, maybe someone can explain it to me in English Well anyway below is the 3 problems I need help with, I need to know what the output would be.

Thanks in Advance

var i = 0;
var favoriteColors = new Array();
favoriteColors[0] = “red”;
favoriteColors[0] = “yellow”;
favoriteColors[0] = “blue”;
favoriteColors[0] = “green”;
favoriteColors[0] = ”fuchsia”;
favoriteColors[0] = “teal”;
while (i < 4) {
document.write(favoriteColors[i] + “<br />”);
++i;
}

var i = 0;
var favoriteColors = new Array();
favoriteColors[0] = “red”;
favoriteColors[0] = “yellow”;
favoriteColors[0] = “blue”;
favoriteColors[0] = “green”;
favoriteColors[0] = ”fuchsia”;
favoriteColors[0] = “teal”;

do{
document.write(favoriteColors[i] + “<br />”);
i = i + 2;
} while(i < favoriteColors.length);


var i = 0;
var favoriteColors = new Array();
favoriteColors[0] = “red”;
favoriteColors[0] = “yellow”;
favoriteColors[0] = “blue”;
favoriteColors[0] = “green”;
favoriteColors[0] = ”fuchsia”;
favoriteColors[0] = “teal”;

for (var i =; i < favoriteColors.length; ++i)
}
document.write(favoriteColors[i] + “<br />”);
}
jwishy is offline   Reply With Quote
Old 03-18-2010, 10:35 PM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,200
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
Well, part of your problem is that you are in the wrong class.

That code you are showing is JavaSCRIPT. So if you are taking a JAVA class, you'll never pass it by coding in JavaScript.

Okay, I teased you enough.

Just for starters, you can't use those kind of curly quote marks in JavaScript.

You must use the ordinary "..." marks.

And then you are assigning *ALL* of those string to the *SAME ELEMENT* of the array.

So the only element you will end up with in the array is "teal". All the other lines are just tossed away.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 03-19-2010, 09:39 AM   PM User | #3
MrEnder
Regular Coder

 
MrEnder's Avatar
 
Join Date: Jul 2008
Location: Canada Ontario
Posts: 258
Thanks: 18
Thanked 4 Times in 4 Posts
MrEnder can only hope to improve
Quote:
Originally Posted by Old Pedant View Post
Well, part of your problem is that you are in the wrong class.

That code you are showing is JavaSCRIPT. So if you are taking a JAVA class, you'll never pass it by coding in JavaScript.

Okay, I teased you enough.

Just for starters, you can't use those kind of curly quote marks in JavaScript.

You must use the ordinary "..." marks.

And then you are assigning *ALL* of those string to the *SAME ELEMENT* of the array.

So the only element you will end up with in the array is "teal". All the other lines are just tossed away.
aww u forgot to tease the part about dreamweaver =[

(sorry couldnt resist posting that it was to tempting)

but anyways

favoriteColors[i] = "red";
__________________
Continually Learning
MrEnder is offline   Reply With Quote
Old 03-19-2010, 09:52 AM   PM User | #4
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,036
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by MrEnder View Post
but anyways

favoriteColors[i] = "red";
NO!

favoriteColors[0] = “red”;
favoriteColors[1] = “yellow”;
favoriteColors[2] = “blue”;
Philip M is offline   Reply With Quote
Old 03-20-2010, 05:13 PM   PM User | #5
jwishy
New to the CF scene

 
Join Date: Mar 2010
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
jwishy is an unknown quantity at this point
Old, you are so right about the crazy quotes. I never noticed that. Should have I used notepad or wordpad instead of word, I cannot remember which lets you save the file in different formats and is used for modyifing code?

Thanks

And whats wrong with Dreamweaver?
jwishy is offline   Reply With Quote
Old 03-20-2010, 05:22 PM   PM User | #6
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,036
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
All files must be plain ASCII text files. Suggest you use Notepad or CuteHTML.


Quote:
Originally Posted by jwishy View Post
And whats wrong with Dreamweaver?
Dreamweaver is not highly regarded in this forum due to the absurdly and excessively complicated code it generates. If you really wish to learn to be a web developer ditch Dreamweaver and use only Notepad or similar.

Be aware that Java and Javascript are entirely different programming languages, in spite of the confusingly similar names.
Philip M is offline   Reply With Quote
Reply

Bookmarks

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 05:46 PM.


Advertisement
Log in to turn off these ads.