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.
Page 1 of 2 12 LastLast
Results 1 to 15 of 27

Thread: Help please

  1. #1
    New Coder
    Join Date
    Mar 2012
    Posts
    18
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Help please

    Here is the question: using a loop, create a website which displays a message box 10 times. Every time the user clicks the message box , the background color is changed to a new color.

    Heres the code I have:

    Code:
    <html>
    <head>
    <title>Colour Changer</title>
    
    <SCRIPT language=JavaScript>
    var color = newarray(10);
    color[0] = red;
    color[1 ]=blue;
    color[2] =yellow;
    color[3]=gray;
    color[4]=purple;
    color[5]=orange;
    color[6]=green;
    color[7]=black;
    color[8]=teal;
    color[9]=pink;
    
    
    
    for (i=0,i<10; i++)
    {
    alert('MsgBox');
    document.bgcolor(color{i});
    }
    
    
    </SCRIPT>
    
    </head>
    
    </html>

  • #2
    Regular Coder
    Join Date
    Apr 2005
    Location
    Texas
    Posts
    448
    Thanks
    24
    Thanked 63 Times in 63 Posts
    your for loop is wrong

    you have
    Code:
    for (i=0,i<10; i++)
    it should be
    Code:
    for (i=0;i<10; i++)
    and just for good measure, you should make the i variable local to the loop with the var keyword
    Code:
    for (var i=0;i<10; i++)
    Allwisend bin ich nicht, doch viel ist mir bewursst
    -Goethe

  • Users who have thanked blaze4218 for this post:

    Thetri (03-15-2012)

  • #3
    The fat guy next door VIPStephan's Avatar
    Join Date
    Jan 2006
    Location
    Halle (Saale), Germany
    Posts
    8,639
    Thanks
    6
    Thanked 1,004 Times in 977 Posts
    This looks pretty much like a school assigment. Please read rule #1.5.
    Last edited by VIPStephan; 03-15-2012 at 04:09 PM.

  • #4
    Regular Coder
    Join Date
    Apr 2005
    Location
    Texas
    Posts
    448
    Thanks
    24
    Thanked 63 Times in 63 Posts
    your array values must be stored as strings

    you have
    Code:
    color[0] = red;
    it should be
    Code:
    color[0] = "red";
    Allwisend bin ich nicht, doch viel ist mir bewursst
    -Goethe

  • Users who have thanked blaze4218 for this post:

    Thetri (03-15-2012)

  • #5
    Regular Coder
    Join Date
    Apr 2005
    Location
    Texas
    Posts
    448
    Thanks
    24
    Thanked 63 Times in 63 Posts
    @VIPStephan does it not look as though they have put forth effort?
    Allwisend bin ich nicht, doch viel ist mir bewursst
    -Goethe

  • Users who have thanked blaze4218 for this post:

    Thetri (03-15-2012)

  • #6
    New Coder
    Join Date
    Mar 2012
    Posts
    18
    Thanks
    10
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by VIPStephan View Post
    This looks pretty much like a school assigment. Please read rule #1.5.
    I was a former school assignment for stupids last year. This year it was given out as a review question. Its not worth any marks, I just want to find the answer or someone to show me whats wrong so I have more study material for my exam.

  • #7
    Regular Coder
    Join Date
    Apr 2005
    Location
    Texas
    Posts
    448
    Thanks
    24
    Thanked 63 Times in 63 Posts
    your array declaration has the two keywords new and Array combined, and not properly capitalized
    Allwisend bin ich nicht, doch viel ist mir bewursst
    -Goethe

  • Users who have thanked blaze4218 for this post:

    Thetri (03-15-2012)

  • #8
    The fat guy next door VIPStephan's Avatar
    Join Date
    Jan 2006
    Location
    Halle (Saale), Germany
    Posts
    8,639
    Thanks
    6
    Thanked 1,004 Times in 977 Posts
    Well, OK, I’m gonna let this get through this time.
    And blaze, the quote in your signature is spelled wrong. It must read: “Allwissend bin ich nicht, doch viel ist mir bewusst”

  • Users who have thanked VIPStephan for this post:

    Thetri (03-15-2012)

  • #9
    Regular Coder
    Join Date
    Apr 2005
    Location
    Texas
    Posts
    448
    Thanks
    24
    Thanked 63 Times in 63 Posts
    It must read as I wrote it. The misspelling is intentional, just think about it for a second.
    Allwisend bin ich nicht, doch viel ist mir bewursst
    -Goethe

  • #10
    New Coder
    Join Date
    Mar 2012
    Posts
    18
    Thanks
    10
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by blaze4218 View Post
    your array declaration has the two keywords new and Array combined, and not properly capitalized
    how about this?

    Code:
    var color = [10];

  • #11
    Regular Coder
    Join Date
    Apr 2005
    Location
    Texas
    Posts
    448
    Thanks
    24
    Thanked 63 Times in 63 Posts
    Quote Originally Posted by Thetri View Post
    how about this?

    Code:
    var color = [10];
    no, you were closer before, you just needed to adjust the spacing and capitalization
    Allwisend bin ich nicht, doch viel ist mir bewursst
    -Goethe

  • #12
    New Coder
    Join Date
    Mar 2012
    Posts
    18
    Thanks
    10
    Thanked 0 Times in 0 Posts
    So I got this now:

    Code:
    <html>
    <head>
    <title>Colour Changer</title>
    
    <SCRIPT language=JavaScript>
    var color = New Array(10);
    color[0] = "red";
    color[1 ]="blue";
    color[2] ="yellow";
    color[3]="gray";
    color[4]="purple";
    color[5]="orange";
    color[6]="green";
    color[7]="black";
    color[8]="teal";
    color[9]="pink";
    
    
    for (var i=0;i<10; i++)
    {
    alert('MsgBox');
    document.bgcolor(color{i});
    }
    
    
    </SCRIPT>
    
    </head>
    
    </html>
    When ever I try to run it I still get a blank page. I'm thinking the MsgBox may be wrong.

  • #13
    Regular Coder
    Join Date
    Apr 2005
    Location
    Texas
    Posts
    448
    Thanks
    24
    Thanked 63 Times in 63 Posts
    this line has many errors
    Code:
    document.bgcolor(color{i});
    1)bgcolor is not the correct DOM reference to change the color of a background
    2)you cannot change the style of the document object, you must change the body, or some other object
    3)to assign a style you must use the = assignment operand
    4)look at how your referencing the array here... how does that differ to how you referenced the array in the rest of the script?

    Edit:
    I stand corrected on the first one,document.body.bgColor="lavender"; is apparently legal js...
    Last edited by blaze4218; 03-15-2012 at 04:28 PM.
    Allwisend bin ich nicht, doch viel ist mir bewursst
    -Goethe

  • #14
    New Coder
    Join Date
    Mar 2012
    Posts
    18
    Thanks
    10
    Thanked 0 Times in 0 Posts
    So I assume its something like this?

    Code:
    document.body.bgColor= "color[i]";

  • #15
    Regular Coder
    Join Date
    Apr 2005
    Location
    Texas
    Posts
    448
    Thanks
    24
    Thanked 63 Times in 63 Posts
    much better, but it is not a string, so loose the quotation marks...
    Allwisend bin ich nicht, doch viel ist mir bewursst
    -Goethe


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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