# Problem with .pos on Gold Rush

**URL:** <https://discourse.codecombat.com/t/problem-with-pos-on-gold-rush/1198>\
**Category:** Uncategorized\
**Created:** [October 17, 2014, 9:21pm UTC](https://discourse.codecombat.com/t/problem-with-pos-on-gold-rush/1198 "2014-10-17T21:21:37Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![BKFighter](https://avatars.discourse-cdn.com/v4/letter/b/e5b9ba/32.png) [@BKFighter](https://discourse.codecombat.com/u/BKFighter)\
**Post date:** [October 17, 2014, 9:21pm UTC](https://discourse.codecombat.com/t/problem-with-pos-on-gold-rush/1198/1 "2014-10-17T21:21:37Z")

</div>

I was having a problem with my code:

```javascript
var items = this.getItems();
var currentShortOfArray = 1;
for(var i=0;i-1<items.length;++i){
    var iPos = items[i];
    var iClose = this.distance(iPos.pos);
    var curClose = this.distance(items[currentShortOfArray].pos);
    if(iClose<curClose){
     currentShortOfArray = i;
    }
}

```

When I press Run, I get an error that says:  
`
Line 16, time 0.0: Cannot read property ‘pos’ of undefined
`  
Does anyone by chance know what I’m doing wrong?

---

<div class="post-metadata">

**Author:** ![BKFighter](https://avatars.discourse-cdn.com/v4/letter/b/e5b9ba/32.png) [@BKFighter](https://discourse.codecombat.com/u/BKFighter)\
**Post date:** [October 17, 2014, 9:25pm UTC](https://discourse.codecombat.com/t/problem-with-pos-on-gold-rush/1198/2 "2014-10-17T21:25:18Z")

</div>

PS  
I am using JavaScript. Forgot to mention that.

---

<div class="post-metadata">

**Author:** ![nick](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/nick/32/784_2.png) [@nick](https://discourse.codecombat.com/u/nick)\
**Post date:** [October 17, 2014, 10:05pm UTC](https://discourse.codecombat.com/t/problem-with-pos-on-gold-rush/1198/3 "2014-10-17T22:05:28Z")

</div>

I think I see the problem. Your for-loop will iterate past the end of the array because of the `i-1` you have. Try just using `i` with no `-1` there. So if items.length is, say, 3, then you will want to access indices 0, 1, and 2, but not 3 (because there’s nothing in index 3–that would be the fourth element).

What are you trying to do with the `currentShortOfArray` variable, by the way?

---

<div class="post-metadata">

**Author:** ![BKFighter](https://avatars.discourse-cdn.com/v4/letter/b/e5b9ba/32.png) [@BKFighter](https://discourse.codecombat.com/u/BKFighter)\
**Post date:** [October 17, 2014, 10:08pm UTC](https://discourse.codecombat.com/t/problem-with-pos-on-gold-rush/1198/4 "2014-10-17T22:08:12Z")

</div>

Ok. the `currentShortOfArray` tells the program which of the objects on the array is the closest. It just holds the number of it. Thanks!

---

<div class="post-metadata">

**Author:** ![BKFighter](https://avatars.discourse-cdn.com/v4/letter/b/e5b9ba/32.png) [@BKFighter](https://discourse.codecombat.com/u/BKFighter)\
**Post date:** [October 17, 2014, 10:11pm UTC](https://discourse.codecombat.com/t/problem-with-pos-on-gold-rush/1198/5 "2014-10-17T22:11:12Z")

</div>

@nick  
Oh, I forgot to include this in the code. The above mention variable is used for this line of code I left out:  
`this.move(items[currentShortOfArray].pos);`  
Now this is the only problem I have. Its same as before. Sorry, I forgot ☹

---

<div class="post-metadata">

**Author:** ![nick](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/nick/32/784_2.png) [@nick](https://discourse.codecombat.com/u/nick)\
**Post date:** [October 17, 2014, 10:31pm UTC](https://discourse.codecombat.com/t/problem-with-pos-on-gold-rush/1198/6 "2014-10-17T22:31:41Z")

</div>

You should check out what’s happening to `currentShortOfArray`, then. You set it to `1` in the beginning, but there may not be two items in the array. You should initialize it to, say, `-1`, and then only move if you found something:

```javascript
if (currentShortOfArray !== -1)
    this.move(items[currentShortOfArray].pos);

```

---

<div class="post-metadata">

**Author:** ![BKFighter](https://avatars.discourse-cdn.com/v4/letter/b/e5b9ba/32.png) [@BKFighter](https://discourse.codecombat.com/u/BKFighter)\
**Post date:** [October 17, 2014, 10:32pm UTC](https://discourse.codecombat.com/t/problem-with-pos-on-gold-rush/1198/7 "2014-10-17T22:32:57Z")

</div>

@nick Thanks. I’ll try that. Thanks again.

---

<div class="post-metadata">

**Author:** ![BKFighter](https://avatars.discourse-cdn.com/v4/letter/b/e5b9ba/32.png) [@BKFighter](https://discourse.codecombat.com/u/BKFighter)\
**Post date:** [October 17, 2014, 10:56pm UTC](https://discourse.codecombat.com/t/problem-with-pos-on-gold-rush/1198/8 "2014-10-17T22:56:51Z")

</div>

Same error on the ` this.move(items[currentShortOfArray].pos);`  
I checked the variables, and all the ones in the for loop are undefined.  
`currentShortOfArray` is some how -1, even though thats not in the code…

---

<div class="post-metadata">

**Author:** ![BKFighter](https://avatars.discourse-cdn.com/v4/letter/b/e5b9ba/32.png) [@BKFighter](https://discourse.codecombat.com/u/BKFighter)\
**Post date:** [October 19, 2014, 12:55am UTC](https://discourse.codecombat.com/t/problem-with-pos-on-gold-rush/1198/9 "2014-10-19T00:55:16Z")

</div>

@nick YES IT WORKS THANK YOU NICK!!! I changed curClose into two lines like the iPos and iClose, and changed the line `var currentShortOfArray = 1;` to `var currentShortOfArray = 0;`. Programming… It perplexes the mind, and the logical system… … . . …

---

<div class="post-metadata">

**Author:** ![BKFighter](https://avatars.discourse-cdn.com/v4/letter/b/e5b9ba/32.png) [@BKFighter](https://discourse.codecombat.com/u/BKFighter)\
**Post date:** [October 19, 2014, 2:07am UTC](https://discourse.codecombat.com/t/problem-with-pos-on-gold-rush/1198/10 "2014-10-19T02:07:28Z")

</div>

I just have one last question… how would you find the gold amount of the other player?

---

<div class="post-metadata">

**Author:** ![nick](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/nick/32/784_2.png) [@nick](https://discourse.codecombat.com/u/nick)\
**Post date:** [October 19, 2014, 6:48pm UTC](https://discourse.codecombat.com/t/problem-with-pos-on-gold-rush/1198/11 "2014-10-19T18:48:02Z")

</div>

`this.getNearestEnemy().gold`

---

<div class="post-metadata">

**Author:** ![BKFighter](https://avatars.discourse-cdn.com/v4/letter/b/e5b9ba/32.png) [@BKFighter](https://discourse.codecombat.com/u/BKFighter)\
**Post date:** [October 19, 2014, 8:43pm UTC](https://discourse.codecombat.com/t/problem-with-pos-on-gold-rush/1198/12 "2014-10-19T20:43:24Z")

</div>

Thank you! That helps
