-1

I have this code

var draw = function() {
var bx = 0;
var by = 0;
//height
for (var i = 0; i < 11; i++) {
    //width
    for (var s = 0; s < 10; s++) { 
        block(bx,by,air);
        bx = bx + 50;
    }
    by = by + 50;
} 

What is happening is when I run draw() it should draw squares in the entire portion of a 10 by 10 plot. However, this is not the case. It is all messed up and I don't know what is happening.

NOTE: this is using processing.js. You can find the complete code here: https://www.khanacademy.org/computer-programming/mc-10/4727460304912384

elixenide
  • 44,308
  • 16
  • 74
  • 100
master
  • 81
  • 12
  • Please provide a more specific description of the problem than "it is all messed up." For example, provide a jsFiddle or codepin.io link. – elixenide Oct 23 '15 at 01:00
  • i put all the code link in the NOTE section – master Oct 23 '15 at 01:03
  • Why don't you just use `bx` and `by` in your for loops? `for(var bx = 0; bx < 550; bx += 50)` – Barmar Oct 23 '15 at 01:04
  • Your question is incomplete. It is going to be closed if you do not improve it, for this reason: Questions seeking debugging help ("**why isn't this code working?**") must include the desired behavior, a *specific problem or error* and *the shortest code necessary* to reproduce it **in the question itself**. Questions without **a clear problem statement** are not useful to other readers. See: [How to create a Minimal, Complete, and Verifiable example.](http://stackoverflow.com/help/mcve) – elixenide Oct 23 '15 at 01:04
  • @EdCottrell The desired behavior is in the question: "it should draw squares in the entire portion of a 10 by 10 plot" – Barmar Oct 23 '15 at 01:05
  • "it is all messed up and i don't know what is happening." With the amount of code you posted, neither do we. Assuming tou actually have the closing brace on your `draw` function, calling it will cause `block` function to be called 110 times. It's not clear what the `air` variable is. Can you clarify "all messed up"? Have you debugged through your code yet? – Krease Oct 23 '15 at 01:06
  • @Barmar Yes, but the problem is not. "It is all messed up" is not what I would call "a specific problem or error." – elixenide Oct 23 '15 at 01:06

2 Answers2

3

Don't use separate variables for the iteration and plotting.

var width = 50;
for (var by = 0; by < 11 * width; by += width) {
    for (var bx = 0; bx < 10 * width; bx += width) {
        block(bx, by, air);
    }
}

or:

var width = 50;
for (var i = 0; i < 11; i++) {
    for (var s = 0; s < 10; s++) { 
        block(s * width, i * width, air);
    }
} 
Barmar
  • 741,623
  • 53
  • 500
  • 612
1

You need to reset bx at every iteration

for (var i = 0; i < 11; i++) {
    bx = 0;
    for (var s = 0; s < 10; s++) { 
        block(bx,by,air);
        bx = bx + 50;
    }
    by = by + 50;
} 
Griffith
  • 3,229
  • 1
  • 17
  • 30