I was having some difficulties setting up a grid using nested if statements. It's a relatively simple task but I seem to be stuck!
This is a school assignment. The instructions are below:
"If the user enters 8 in the text input and clicks the button, you should draw eight rows and columns of little squares. To do this, you will need two for loops nested inside each other like this:
for ( row=1; ... ) {
for ( col=1; ... ) {
...
}
}
In the (inner-most) loop body, draw a little square on the paper with an x and y value calculated from the loop counters so the squares end up in a grid pattern.
Your page will look something like this (depending on the number the user enters, obviously). Don't worry about the coloured boxes yet: that is the next part."
The outcome should look something like this:
You can ignore the fact that some of squares are colored. So far I'm come up with this:
generate = function() {
n = 0
x = 0
y = 0
n = $('#squares').val()
for (row = 1; row <= n; row += 1) {
for (col = 1; col <= n; col += 1) {
r = paper.rect(x, y, 10,10)
x = x + 20
}
y = y + 20
}
}
setup = function() {
paper = Raphael('container', 400, 400)
$('#number').click(generate)
}
jQuery(document).ready(setup)
Thank you in advance!
paper.rect()? One problem that you do have it that you are incrementingxby 20 each time in the inner loop, but you would have to reset it to the 0 position for each new row, for example.