Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Mar 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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 />”);
    }

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,184
    Thanks
    75
    Thanked 4,340 Times in 4,306 Posts
    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.

  • #3
    Regular Coder MrEnder's Avatar
    Join Date
    Jul 2008
    Location
    Canada Ontario
    Posts
    258
    Thanks
    18
    Thanked 4 Times in 4 Posts
    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

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,918
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by MrEnder View Post
    but anyways

    favoriteColors[i] = "red";
    NO!

    favoriteColors[0] = “red”;
    favoriteColors[1] = “yellow”;
    favoriteColors[2] = “blue”;

  • #5
    New to the CF scene
    Join Date
    Mar 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,918
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •