# For loop is not being called

**URL:** <https://discourse.processing.org/t/for-loop-is-not-being-called/21493>\
**Category:** Coding Questions\
**Created:** [June 2, 2020, 12:03am UTC](https://discourse.processing.org/t/for-loop-is-not-being-called/21493 "2020-06-02T00:03:46Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![shodai](https://avatars.discourse-cdn.com/v4/letter/s/50afbb/32.png) [@shodai](https://discourse.processing.org/u/shodai)\
**Post date:** [June 2, 2020, 12:03am UTC](https://discourse.processing.org/t/for-loop-is-not-being-called/21493/1 "2020-06-02T00:03:46Z")

</div>

I am working through a snake game program and have run into a really odd problem. Everything works except that the snake does not crash into itself. When the snake runs into itself the screen should turn red temporarily. The specific problem is on line 55. There is a for loop that for some reason is not running. I am really confused as I do not see the problem. Can anyone please help?

```auto
let numOfBlocks = 20;
let blockSize = 20;
let headX = 0;
let headY = 0;
let speedX = 0;
let speedY = 0;
let tailLength = 0;
let tailBlocks = [];
let appleX = 0;
let appleY = 0;
let game = true;

function setup() {
  createCanvas(400, 400);
  frameRate(10);
  //start snake at center
  headX = numOfBlocks / 2;
  headY = numOfBlocks / 2;
  //starting location of the apple
  appleX = floor(random(0, numOfBlocks));
  appleY = floor(random(0, numOfBlocks));
}

function draw() {
  if (game == true) {
    background("black");
    // the snake
    fill("white");
    rect(headX * blockSize, headY * blockSize, blockSize, blockSize);
    headX = headX + speedX;
    headY = headY + speedY;
    //the apple  
    fill("red");
    rect(appleX * blockSize, appleY * blockSize, blockSize, blockSize);
    // snake touches apple  
    if (headX == appleX && headY == appleY) {
      appleX = floor(random(0, numOfBlocks));
      appleY = floor(random(0, numOfBlocks));
      tailLength = tailLength + 1;
    }
    // the tail  
    tailBlocks.push({
      x: headX,
      y: headY
    })
    for (let i = 0; i < tailBlocks.length; i++) {
      fill("white");
      rect(tailBlocks[i].x * blockSize, tailBlocks[i].y * blockSize, blockSize, blockSize);
    }
    // tail cutter
    while (tailBlocks.length > tailLength) {
      tailBlocks.shift();
    }
    // touching your own tail  
    for (let z = 0; z < tailLength.length; z++) {
      if (headX + speedX == tailBlocks[z].x && headY + speedY == tailBlocks[z].y) {
        background("red");
        game = false;
      }

    }

    // the walls
    if (headX < 0) {
      headX = numOfBlocks - 1;
    }
    if (headX > numOfBlocks - 1) {
      headX = 0;
    }
    if (headY < 0) {
      headY = numOfBlocks - 1;
    }
    if (headY > numOfBlocks - 1) {
      headY = 0;
    }

  }
  // what happens when the game ends
  if (game == false) {
    background("red");

    textSize(50);
    text("Game Over", 70, 200);
    textSize(30);
    text("Try Again?", 130, 275);
    if (mouseIsPressed) {
      game = true;
    }
  }

}

function keyPressed() {
  // the controls
  if (key == "w" || keyIsDown(UP_ARROW)) {
    speedX = 0;
    speedY = -1;
  }
  if (key == "a" || keyIsDown(LEFT_ARROW)) {
    speedX = -1;
    speedY = 0;
  }
  if (key == "s" || keyIsDown(DOWN_ARROW)) {
    speedX = 0;
    speedY = 1;
  }
  if (key == "d" || keyIsDown(RIGHT_ARROW)) {
    speedX = 1;
    speedY = 0;
  }

}

```

---

<div class="post-metadata">

**Author:** ![SomeOne](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/someone/32/8639_2.png) [@SomeOne](https://discourse.processing.org/u/SomeOne)\
**Post date:** [June 2, 2020, 5:05am UTC](https://discourse.processing.org/t/for-loop-is-not-being-called/21493/2 "2020-06-02T05:05:55Z")

</div>

> [@shodai](#):
>
> `let tailLength = 0;`

```auto
for (let z = 0; z < tailLength.length; z++) {

```

I don’t know if variable has property `.length` or not, but that’s the problem why loop doesn’t work

---

<div class="post-metadata">

**Author:** ![shodai](https://avatars.discourse-cdn.com/v4/letter/s/50afbb/32.png) [@shodai](https://discourse.processing.org/u/shodai)\
**Post date:** [June 2, 2020, 1:15pm UTC](https://discourse.processing.org/t/for-loop-is-not-being-called/21493/3 "2020-06-02T13:15:48Z")

</div>

It’s strange because I have done this exact code before and have ha no issues with it.

---

<div class="post-metadata">

**Author:** ![SomeOne](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/someone/32/8639_2.png) [@SomeOne](https://discourse.processing.org/u/SomeOne)\
**Post date:** [June 2, 2020, 1:50pm UTC](https://discourse.processing.org/t/for-loop-is-not-being-called/21493/4 "2020-06-02T13:50:45Z")

</div>

Take a closer look.

```auto
for (let i = 0; i < tailBlocks.length; i++) {

```

tailBlocks is an array that has lenght property

```auto
for (let z = 0; z < tailLength.length; z++)

```

tailLenght is a numeral so it hasn’t length property

---

<div class="post-metadata">

**Author:** ![shodai](https://avatars.discourse-cdn.com/v4/letter/s/50afbb/32.png) [@shodai](https://discourse.processing.org/u/shodai)\
**Post date:** [June 2, 2020, 1:56pm UTC](https://discourse.processing.org/t/for-loop-is-not-being-called/21493/5 "2020-06-02T13:56:35Z")

</div>

What you are saying makes perfect sense. The issue that I am having is that this code is based on a tutorial; a tutorial that I completed months ago. I am trying to do it again and now it is not working. But when I compare this code to the code that I did originally, the code is the same and my old code works fine and this new attempt does not work. So I am really confused.

---

<div class="post-metadata">

**Author:** ![shodai](https://avatars.discourse-cdn.com/v4/letter/s/50afbb/32.png) [@shodai](https://discourse.processing.org/u/shodai)\
**Post date:** [June 2, 2020, 2:12pm UTC](https://discourse.processing.org/t/for-loop-is-not-being-called/21493/6 "2020-06-02T14:12:00Z")

</div>

Sorry, sorry, sorry. I finally see it. I was using the wrong variable, which is the error that you were pointing out. I am really sorry for my error.

---

<div class="post-metadata">

**Author:** ![SomeOne](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/someone/32/8639_2.png) [@SomeOne](https://discourse.processing.org/u/SomeOne)\
**Post date:** [June 2, 2020, 4:43pm UTC](https://discourse.processing.org/t/for-loop-is-not-being-called/21493/7 "2020-06-02T16:43:25Z")

</div>

It’s sometimes hard to see errors. You have an idea what you were doing and you just cannot see that code is not what you meant it to be. With practise and time you’ll learn. And there’s no reason to be sorry, when you learning 😃
