0

It's just making a red line along the top and left edge and I don't understand why. Shouldn't the nested for loops iterate through every possible x,y coordinate?

function createSquare() {
    var height = 50;
    var width = 50;
    var img = new PNGlib(width, height, 256);
    var background = img.color(0, 0, 0, 0);

    for (var x = 0; x <= width; x ++) {
        for (var y = 0; y <= height; y ++) {
            img.buffer[img.index(x, y)] = img.color(0xFF, 0x00, 0x00);
        }
    }

    return ('<img src="data:image/png;base64,' + img.getBase64() + '">');
}
JOATMON
  • 15,576
  • 31
  • 99
  • 188
  • 2
    Why `<=` instead of just ` – raina77ow Jun 24 '12 at 20:14
  • oh... why the comment instead of the answer? :D – JOATMON Jun 24 '12 at 20:18
  • So what is the output @ScottBeeson? – Ryan Jun 24 '12 at 20:18
  • Not sure why it didn't hit me that it still increments after it meets the condition, so < is what I wanted, and it fixed the problem. Please post as answer so I can credit you @raina77ow – JOATMON Jun 24 '12 at 20:19
  • Never mind, it's one of the most common types of bugs I've seen in codebase I worked with. That's why I usually fix these almost automatically - and write some big comment explaining why `<=` is used in some particular case, if that turns out to be not a bug. ) – raina77ow Jun 24 '12 at 20:22

1 Answers1

1

I never used PNGLib (would be interesting to try, though), but from my experience <= used in for loop exit condition is almost always an error. )

raina77ow
  • 91,589
  • 12
  • 180
  • 210