Hi, I got problem with that level, this is my code. can anyone help me
while (true) {
var closestGold = null;
var minGoldDist = Infinity;
var coinIndex = 0;
var coins = hero.findItems();
while (coinIndex < coins.length) {
var coin = coins[coinIndex];
}
if (coin) {
distance = hero.distanceTo(coin);
}
if (distance < minGoldDist && coin.value == 3) {
closestGold = coin;
minGoldDist = distance;
}
coinIndex += 1;
}
if (closestGold) {
hero.moveXY(closestGold.pos.x, closestGold.pos.y);
}
thx a lot for any help
I see a few problems with the code. First, your indentation needs some serious attention. Second, you have an errant closing bracket ‘}’ on line nine…it belongs elsewhere. Third, you’ve missed a step in defining ‘distance’.
while (true) {
var closestGold = null;
var minGoldDist = Infinity;
var coinIndex = 0;
var coins = hero.findItems();
}
while (coinIndex < coins.length) {
var coin = coins[coinIndex];
if (coin) {
var distance = hero.distanceTo(coin);
if (distance < minGoldDist && coin.value == 3) {
closestGold = coin;
distance = minGoldDist;
}
coinIndex += 1;
}
if (closestGold) {
hero.moveXY(closestGold.pos.x, closestGold.pos.y);
}
}
and it still doesnt work :(
Deadpool198 thx 4 Your help my problem is that I dont understand much what You write to me I change my code and there is still a problem my hero collect only 2 coins and third, not , my code now looks like this:
please help, I passed this level on python but I wanna to learn how to do this in javascript
while (true) {
var closestGold = null;
var minGoldDist = Infinity;
var coinIndex = 0;
var coins = hero.findItems();
while (coinIndex < coins.length) {
var coin = coins[coinIndex];
if (coin) {
var distance = hero.distanceTo(coin);
if (distance < minGoldDist && coin.value == 3) {
distance = minGoldDist;
}
closestGold = coin;
}
coinIndex += 1;
}
if (closestGold) {
hero.moveXY(closestGold.pos.x, closestGold.pos.y);
}
}
You should put closestGold back inside the if statement, like you had it before.
Again the problem is here:
distance = minGoldDist;
Are you sure that’s the right way round? Think about what distance is, that’s your distance to the coin, and you change it every loop.
Don’t you think you should define minGoldDist using distance instead? (switch the order around)
Danny
Think about it like this:
minGoldDistance is the variable you use to keep track of the closest coin.
distance is a variable you change every loop of the while loop which tells you how far away the coin is.
You want to update minGoldDistance with distance if distance is smaller than minGoldDistance so that minGoldDistance contains the smallest distance overall at the end. At the moment your redefining distance with the value of minGoldDistance.
var minGoldDistance = Infinity; // infinity
var distance = hero.distanceTo(coin); // something like 8
if (distance < minGoldDist && coin.value == 3) { // 8 is less than infinity
closestGold = coin; // this is true
distance = minGoldDist; // now distance has the value of "infinity" & minGoldDist is unchanged.
}