Greed Traps Help


I’ve tried this code many times, and I think it’s right, but the first enemy that comes my character doesn’t build a fire-trap for. All of the other ones it does. Could I have some help please?
Thanks in advance!

// Patrol around the clearing and place traps ONLY if you see a coin.
// Move from one mark to another moving clockwise and avoiding the coins.

// Write this function.
function maybeBuildTrap(x, y) {
    // Move to the x,y postion and build a 'fire-trap' if a coin is near.
    var item = hero.findNearestItem();
    hero.moveXY(x, y);
    if (item) {
        hero.buildXY("fire-trap", x, y);

while (true) {
    // Call 'maybeBuildTrap' function for the top left passage.
    maybeBuildTrap(12, 56);
    // Now for the top right passage.
    maybeBuildTrap(68, 56);
    // Now for the bottom right passage.
    maybeBuildTrap(68, 12);
    // Now for the bottom left passage.
    maybeBuildTrap(12, 12);


Try to move first, then search an item.