# Snake: Will not ad cells to array with push. Why not?

**URL:** <https://discourse.processing.org/t/snake-will-not-ad-cells-to-array-with-push-why-not/9890>\
**Category:** Coding Questions\
**Created:** [April 2, 2019, 8:21pm UTC](https://discourse.processing.org/t/snake-will-not-ad-cells-to-array-with-push-why-not/9890 "2019-04-02T20:21:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![steinlav](https://avatars.discourse-cdn.com/v4/letter/s/8491ac/32.png) [@steinlav](https://discourse.processing.org/u/steinlav)\
**Post date:** [April 2, 2019, 8:21pm UTC](https://discourse.processing.org/t/snake-will-not-ad-cells-to-array-with-push-why-not/9890/1 "2019-04-02T20:21:57Z")

</div>

I am translating from Processing:  
[http://vestera.as/processing/?id=23](http://vestera.as/processing/?id=23)

to p5.js:  
[https://editor.p5js.org/steinlav/sketches/1rkgu\_AGI](https://editor.p5js.org/steinlav/sketches/1rkgu_AGI)

Problem in p5.js line 57  
Why is it not possible to ad object “head” to the array “body”?  
I have stared at this for hours ☹🙂

```auto
function update() {
  // Puts the head in the array. Why does this not work?
  **body.push(head);**
  if (grow > 0) { 
    // Shall grow, Head is moving. Tail stays behind.
    grow--;  
  } else { 
    // Removing tail cell
    body.splice(0, 1); 
  }

```

---

<div class="post-metadata">

**Author:** ![figraham](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/figraham/32/4161_2.png) [@figraham](https://discourse.processing.org/u/figraham)\
**Post date:** [April 2, 2019, 10:32pm UTC](https://discourse.processing.org/t/snake-will-not-ad-cells-to-array-with-push-why-not/9890/2 "2019-04-02T22:32:26Z")

</div>

It just looks like a small typo. I started by `console.log(body)` to see if the push was working. The body was growing so. It’s not displaying right. The for loop has a typo in it.

```auto
// on line 46
for (i = 0; i < body.lenght; i++) {
// should be change to
for (let i = 0; i < body.length; i++) {

```

This is one of the disadvantages of javascript where it won’t always tell you if a object key doesn’t exist.

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [April 3, 2019, 12:05am UTC](https://discourse.processing.org/t/snake-will-not-ad-cells-to-array-with-push-why-not/9890/3 "2019-04-03T00:05:56Z")

</div>

> [@figraham](#):
>
> This is one of the disadvantages of JS where it won’t always tell you if an object key doesn’t exist.

> **[Strict mode - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode#Converting_mistakes_into_errors)**
>
> JavaScript's strict mode is a way to opt in to a restricted variant of JavaScript, thereby implicitly opting-out of "sloppy mode". Strict mode isn't just a subset: it intentionally has different semantics from normal code. Browsers not supporting...

---

<div class="post-metadata">

**Author:** ![steinlav](https://avatars.discourse-cdn.com/v4/letter/s/8491ac/32.png) [@steinlav](https://discourse.processing.org/u/steinlav)\
**Post date:** [April 3, 2019, 5:42am UTC](https://discourse.processing.org/t/snake-will-not-ad-cells-to-array-with-push-why-not/9890/4 "2019-04-03T05:42:28Z")

</div>

🤪 Thank you!! 🙂
