Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Javascript Problem ;/

Options
  • 23-02-2005 1:46pm
    #1
    Registered Users Posts: 3,945 ✭✭✭


    Hey,

    Started doing JS about 2 weeks ago in my course. Anyway got this problem, I've to create a checkerboard type pattern out of asterix. So I've a line of 8 asterix ("* ") and a space between them (" "). Then it goes to the next row which is indented to make the checkboard effect.

    I can only use the following statements, "document.write( "* " );", "document.write( " " );" and "document.writeIn( "<BR>" );" and I can only use each once.

    So far I've got :
    var check = 0;
    check2 = 0;

    for( check = 0 ; check <= 7 ; check++ ) {
    for ( check2 = 0 ; check2 <= 8 ; check2++) {
    document.write( "* " );
    document.write( " " );
    }
    document.write( "<br>" );
    }

    I'm not quite sure how to dent the second line in as you can see. Our teacher hasn't told us how to do it yet, but just told us to attempt it. Could someone please point me in the right direction? I'd like to finish it myself but I can't :(


Comments

  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    Edit:

    Bluergh.

    Missed the "indent" bit. So you want:
    * * * * * * * * * 
     * * * * * * * * * 
    * * * * * * * * *
    
    Etc?
    Think about:
    1. What value do you have to test to see if you are starting on an even row?
    2. What do you need to do if you it is an even row?


  • Registered Users Posts: 6,508 ✭✭✭daymobrew


    I has *a* solution which uses an additional 'document.write( " " );' so it fails the and I can only use each once. rule.
    In case you haven't done this, put the code inside '<pre>' and '</pre>' tags so the spaces don't 'disappear' because of the use of proportional fonts.

    I won't post my 'solution' because it breaks the 'once' rule, because this is a class thing, though I will say I used the '%' operator to determine when I was an alternate row.

    Aside: I changed the variable names to 'row' and 'column' to make the code a little clearer.


  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    I also missed the "Can only use once rule" :rolleyes:

    I have a solution, but you need to replace document.write(" ") with document.write(" ") because there's just no other way to display two concurrent spaces, or a space at the start of a line in an HTML document.

    It uses the % operator and some trickery.


Advertisement