Hi there, same issue… I’m not understanding what is going on with this code…

var coins = hero.findItems();
var coinIndex = 0;
var nearest = null;
var nearestDistance = 9999;
// Loop through all the coins to find the nearest one.
while(coinIndex < coins.length) {
var coin = coins[coinIndex];
coinIndex++;
var distance = hero.distanceTo(coin);
// If this coin's distance is less than the nearestDistance
if (distance < nearestDistance) {
// Set nearest to coin
nearest = coin;
// Set nearestDistance to distance
nearestDistance = distance;
}
}

Can someone give me a super basic break down of what’s happening?

Basicaly, the code is to compare the distance of each coins and return the nearest coin
I don’t write java but I can tell

var coins is an array/list
coinIndex will be the increment to check each item in your array/list
var nearest is the variable where the nearest coin will be
and NearestDistance is a maximal value possible

coins.length even tho different from python return the amount of item in the array

var coin = coins[coinIndex]
if coinindex = 0 then coin is equal to the first item in coins, coins[0]

then coinIndex is incremented by 1 with this code coinIndex++;
so the next time the loop happen it will get the second item in the list. and so on

after that they get the distance of the coin to the hero.

so you need to compare the distance to the maximal value to return the nearest coin.

if the distance is lower then the maximal distance you need to say that the maximal distance is now the coin distance,

and after that you need to change the nearest value which is none to the coin.
once the loop finishes you will have the nearest coin.

Those kind of problem can be solved many ways by the way

var distance = hero.distanceTo(target);
if (distance > maxDistance) {
maxDistance = distance;
farthest = target;
}
}

I’m understanding that it is finding < || > but I don’t get how it is finding the end value? I guess I’m understanding what I am reading but not getting the process of elimination it’s using to find that return value. (OOOOhhh I just read through the code and what you said again and think I got it!)

Alright so let me just type this out!
if the distance is less than 9999, the null value becomes the coin, the distance to that coin is stored as the nearest distance. it proceeds to cycle through the remaining array if the value is lesser than the value is store, if it is greater than it passes the value. the and value will be what it returns. (or the null value)

So 2 quick questions to branch off on this. why do I add to the index prior to finding if distance < nearestDistance? If my array starts at 0 wouldn’t that start it at 1? and keep adding each consecutive function? Then when it hits the end of the line it would meet the condition of the while loop effectively ending the code skipping the first and last items in the array?

second, is it crucial to set the variable nearest = null? I understand now that it exempts me from defining the variable later on as it is already defined but couldn’t I just type “var nearest = coin;” rather than “var nearest = null;” and redefining it down the road?

<- cool I didn’t know I could see people replying lol, sorry for being a creep ^^

not sure to understand well the question but ill try answering

you can add EnemyIndex += 1 after var coin is defined or even after the if.
the reason it works is because coin is already defined after and you compare the distance of the coin and not the value of coins[coinIndex]

And for the null variable I believe it is optional