Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Javascript Problem ;/

  • 23-02-2005 01:46PM
    #1
    Registered Users, Registered Users 2 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, Registered Users 2 Posts: 68,173 ✭✭✭✭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, Registered Users 2 Posts: 6,652 ✭✭✭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, Registered Users 2 Posts: 68,173 ✭✭✭✭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