# How do loop something?

**URL:** <https://forum.code.org/t/how-do-loop-something/36842>\
**Category:** Coding and Debugging Help\
**Created:** [September 6, 2022, 1:27pm UTC](https://forum.code.org/t/how-do-loop-something/36842 "2022-09-06T13:27:29Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![pluto](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/pluto/32/9141_2.png) [@pluto](https://forum.code.org/u/pluto)\
**Post date:** [September 6, 2022, 1:27pm UTC](https://forum.code.org/t/how-do-loop-something/36842/1 "2022-09-06T13:27:29Z")

</div>

I know the general basics of a loop, but I’m coding a game where you have to buy something then something would loop. Ex:

```auto
var x = 0
var z = 0
function functionhere() { 
if (mouseIsOver(sprite) && mouseWentDown("leftButton")) {
      x = x + 1
 }
if (x == 1) {
      for (var y = 0; y < longnumberhere; y++) {
          z = z + 1
  }
 }
}
//it should loop i think but i dont why it doesnt work

```

---

<div class="post-metadata">

**Author:** ![wutadam](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/wutadam/32/7711_2.png) [@wutadam](https://forum.code.org/u/wutadam)\
**Post date:** [September 6, 2022, 2:13pm UTC](https://forum.code.org/t/how-do-loop-something/36842/2 "2022-09-06T14:13:32Z")

</div>

Put it in the draw() function.

---

<div class="post-metadata">

**Author:** ![pluto](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/pluto/32/9141_2.png) [@pluto](https://forum.code.org/u/pluto)\
**Post date:** [September 6, 2022, 2:42pm UTC](https://forum.code.org/t/how-do-loop-something/36842/3 "2022-09-06T14:42:49Z")

</div>

I’m pretty sure that wouldn’t work and plus I do have it in there, but I didn’t show it.

---

<div class="post-metadata">

**Author:** ![wutadam](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/wutadam/32/7711_2.png) [@wutadam](https://forum.code.org/u/wutadam)\
**Post date:** [September 6, 2022, 2:44pm UTC](https://forum.code.org/t/how-do-loop-something/36842/4 "2022-09-06T14:44:15Z")

</div>

Is the functionhere() function being called?

---

<div class="post-metadata">

**Author:** ![pluto](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/pluto/32/9141_2.png) [@pluto](https://forum.code.org/u/pluto)\
**Post date:** [September 6, 2022, 4:25pm UTC](https://forum.code.org/t/how-do-loop-something/36842/5 "2022-09-06T16:25:00Z")

</div>

I just did it as a replacement for draw for no absolute reason I did actually put draw, like I said I put it on there. Also it’s a draw function it probably does not need to be called.

---

<div class="post-metadata">

**Author:** ![mwood](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/mwood/32/4941_2.png) [@mwood](https://forum.code.org/u/mwood)\
**Post date:** [September 6, 2022, 9:54pm UTC](https://forum.code.org/t/how-do-loop-something/36842/6 "2022-09-06T21:54:41Z")

</div>

If you can share your code with us, we can look at it and try to help you debug.

thanks,

Mike

---

<div class="post-metadata">

**Author:** ![pluto](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/pluto/32/9141_2.png) [@pluto](https://forum.code.org/u/pluto)\
**Post date:** [September 6, 2022, 9:56pm UTC](https://forum.code.org/t/how-do-loop-something/36842/7 "2022-09-06T21:56:15Z")

</div>

I basically just did.

---

<div class="post-metadata">

**Author:** ![mwood](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/mwood/32/4941_2.png) [@mwood](https://forum.code.org/u/mwood)\
**Post date:** [September 6, 2022, 10:09pm UTC](https://forum.code.org/t/how-do-loop-something/36842/8 "2022-09-06T22:09:53Z")

</div>

Sorry. I meant if you share a link to your code, we can try it. It’s ok if you don’t want to share as well; It’s sometimes just harder to debug if we can’t play with it a little.

My thought was similar to @wutadam in that the function needs to be called. Even if it is in the draw loop, it may not just run automatically, it may need to be called directly before it runs.

Even if you wanted to put that code into a program that is separate from the game you are making and just share that link, I would be happy to look at it…

Mike

---

<div class="post-metadata">

**Author:** ![mwood](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/mwood/32/4941_2.png) [@mwood](https://forum.code.org/u/mwood)\
**Post date:** [September 6, 2022, 10:17pm UTC](https://forum.code.org/t/how-do-loop-something/36842/9 "2022-09-06T22:17:02Z")

</div>

@pluto,

Here’s a link to a project that mostly uses your code. I just added the text to display the value of X.

I created the function outside of the draw loop and called it inside the draw loop and it is appearing to work.

Does this help?

> **[Check out what I made](https://studio.code.org/projects/gamelab/kysGvIDALdwVNkfAHDTghHvPKr0_mgBpHxabqYyKSfs)**
>
> I wrote the code myself with Code.org

Let us know if not!

Thanks,

Mike

---

<div class="post-metadata">

**Author:** ![pluto](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/pluto/32/9141_2.png) [@pluto](https://forum.code.org/u/pluto)\
**Post date:** [September 6, 2022, 10:25pm UTC](https://forum.code.org/t/how-do-loop-something/36842/10 "2022-09-06T22:25:12Z")

</div>

I debugged it by myself somehow, but thanks! If I caused any inconvenience I’m sorry.
