-3

I figured it out but it sure took me 4 hours. It never caused any errors so I used the debug feature which wasn't much help. Since there was no error I'm unsure what else to look up before I ask my question.

for (var i = 0; i < 300; i+7) { //30:00
        var random_x = getRandomInt(0, width-1);
        var random_y = getRandomInt(0, height-1);
        var sample_color = img.colorAt(random_x, random_y);

Solution: change i+7to i++

I'm still confused on why i+7 works when number of loops are specified but not when ran with infinity loop.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
BlueHits
  • 3
  • 1
  • 2
    You need to assign the incremented value back to the variable: `i += 7`. With your approach, you've effectively created an infinite loop because the value of `i` never changes. – Robby Cornelissen Jun 29 '22 at 05:52

2 Answers2

3

The problem with your code is that you aren't assigning the value to i again.

When you write i++ you basically write a shorthand version of i = i + 1. In your code you write i + 7 which doesn't do anything and basically is an infinite loop. You should have written i = i + 7 to assign a new value to i (or i += 7 for the shorthand version).

M. Deinum
  • 115,695
  • 22
  • 220
  • 224
1

either use i=i+7 or i+=7

i=i+7 means you are changing the value of i by adding 7 to it. So after every increment value of i increment by 7.

For better knowledge, refer assignment operators.

  • Okay that clears up some, but i'm still confused on why `i+7` works with a numbered loop without rewritting `i = i + 7` – BlueHits Jun 29 '22 at 14:22
  • you are not assigning/incrementing the value i+7 to i, if you want to increment the value you need to assign the = (equal)operator – Cinthya M Shaji Jul 18 '22 at 16:23