# Odd behavior on "DANGER! MINEFIELD" when calling self declared function

Here’s my code:

``````this.detonate = function(i) { this.say("Detonate " + i, [i]); };
var coins = this.getItems();

var values = [];
for (var i = 2; i <= 120; ++i)
{
values.push(i);
}

this.isPrime = function(v)
{
for (var j = 0; j < values.length; ++j)
{
if (v === values[j])
{
return true;
}
else if ((v / values[j]) % 1 === 0)
{
return false;
}
}

return true;
};

for (var i = 0; i < values.length; ++i)
{
if (this.isPrime(values[i]) === false)
{
this.detonate(values[i]);
}
}

this.say("Woohoo, safe! Lootin' time!");  // (do not remove)
this.move(coins[0].pos);
``````

If I check that my number is not a prime with the function it will drop multiple ogres in the same location. Because it drops several ogres in the same spot, the whole simulation takes longer than allocated (around mine 96 at the 7:30 minute mark). If I move the prime code from the function inside the for loop everything works as expected (one ogre per mine).

Code that works:

``````this.detonate = function(i) { this.say("Detonate " + i, [i]); };
var coins = this.getItems();

var values = [];
for (var i = 2; i <= 120; ++i)
{
values.push(i);
}

for (var i = 0; i < values.length; ++i)
{
var prime = true;

for (var j = 0; j < values.length; ++j)
{
if (values[i] === values[j])
{
break;
}
else if ((values[i] / values[j]) % 1 === 0)
{
prime = false;
break;
}
}

if (prime === false)
{
this.detonate(values[i]);
}
}

this.say("Woohoo, safe! Lootin' time!");  // (do not remove)
this.move(coins[0].pos);
``````

I guess that is not the only bug in this level. When i called ‘detonate’ inside coins.forEach, nothing was synchronized. Most of the detonations were ignored because the griffin was busy at the moment.

Another problem is that coin indices are not changed when you detonate them, but after you done with it, all indices are changed, so you have to collect all coins, not only the ones with prime indices.

And this ‘looting time’ that i shall not remove…it’s just weird. Why is it in the editor at all if we shall not remove it?

Sorry guys, there are two bugs on my end that are making this level really picky:

https://github.com/codecombat/codecombat/issues/1136 makes it so that calling extra methods causes time-taking methods to be called again.
https://github.com/codecombat/aether/issues/49 makes it so that we can’t make time-taking methods yield within a nested function.

Once I solve both of those, you won’t see those problems, and we won’t need the “Lootin’ time” statement.

I found a strange behavior on this level, not linked with the one above.
(do you prefere I open another thread ?)

I get a success, but I tried to optimise to final coin collection.
And then I was not able to get the coin for the number 47. All others,ok, but not this one.
If you want to check my code (looks at the end, I went back on 47 and the guy goes to 46) :

I had the exact same problem as you Vettax. I ended up hard coding this.move at the end to just get it over and done with.