Drop the flag_HELP! (Java script)

I have been stuck on this level for quite a while now…PLEASE HELP. This is my code so far and the picture and the link:

link_https://codecombat.com/play/level/drop-the-flag
My code

// Put flags where you want to build traps.
// When you’re not building traps, pick up coins!

while(true) {
var flag = hero.findFlag();
if (flag) {
// How do we get flagX and flagY from the flag’s pos?
// (Look below at how to get x and y from items.)
var itemPos = item.pos;
var itemX = itemPos.x;
var itemY = itemPos.y;
hero.moveXY(itemX, itemY);

    var flag = hero.findFlag("green");
    hero.buildXY("fire-trap", flagX, flagY);
    hero.pickUpFlag(flag);
}
else {
    var item = hero.findNearestItem();
    if (item) {
        var itemPos = item.pos;
        var itemX = itemPos.x;
        var itemY = itemPos.y;
        hero.moveXY(itemX, itemY);
    }
}

}

this is what it looks like

Put it all in loop and put all var under while loop

What do you mean? I already have a loop on it.

while(true) {
var flag = hero.findFlag();
if (flag) {
// How do we get flagX and flagY from the flag’s pos?
// (Look below at how to get x and y from items.)
var itemPos = item.pos;
var itemX = itemPos.x;
var itemY = itemPos.y;
hero.moveXY(itemX, itemY);

var flag = hero.findFlag("green");
hero.buildXY("fire-trap", flagX, flagY);
hero.pickUpFlag(flag);

}
else {
var item = hero.findNearestItem();
if (item) {
var itemPos = item.pos;
var itemX = itemPos.x;
var itemY = itemPos.y;
hero.moveXY(itemX, itemY);
}
}
}

Would you mind hilighting the part where you are talking about for me?

Problem is that you said if item instead of finding the flag so your item is undefined.

Look below:

while(true) {
var flag = hero.findFlag();
if (flag) {
// How do we get flagX and flagY from the flag’s pos?
// (Look below at how to get x and y from items.)
var itemPos = item.pos;
var itemX = itemPos.x;
var itemY = itemPos.y;
hero.moveXY(itemX, itemY);

var flag = hero.findFlag(“green”);
hero.buildXY(“fire-trap”, flagX, flagY);
hero.pickUpFlag(flag);
}

So you didn’t define Item, cause you defined flag, so in your code you’re finding item instead of flag.

1 Like

Got it fixed???

No everytime i reset it so that could try te code over again it automaticlly tells me its wrong…

hero.buildXY("fire-trap", flagX, flagY);

That this is wromg.
this is code on reset:

// Put flags where you want to build traps.
// When you’re not building traps, pick up coins!

while(true) {
var flag = hero.findFlag();
if (flag) {
// How do we get flagX and flagY from the flag’s pos?
// (Look below at how to get x and y from items.)

    hero.buildXY("fire-trap", flagX, flagY);
    hero.pickUpFlag(flag);
}
else {
    var item = hero.findNearestItem();
    if (item) {
        var itemPos = item.pos;
        var itemX = itemPos.x;
        var itemY = itemPos.y;
        hero.moveXY(itemX, itemY);
    }
}

}

Write your code and take a screenshot and send it here to me.

How do we get flagX and flagY from the flag’s pos?

flagX=flag.pos.x
flagY=?

What do you mean everytime i RESET THE GAME is when it happens. For somereason it does this and i never understand why plus its always wrog…

dude I’m saying that look you’re code

while(true) {
var flag = hero.findFlag();
if (flag) {
// How do we get flagX and flagY from the flag’s pos?
// (Look below at how to get x and y from items.)
var itemPos = item.pos;
var itemX = itemPos.x;
var itemY = itemPos.y;
hero.moveXY(itemX, itemY);

var flag = hero.findFlag(“green”);
hero.buildXY(“fire-trap”, flagX, flagY);
hero.pickUpFlag(flag);
}
else {
var item = hero.findNearestItem();
if (item) {
var itemPos = item.pos;
var itemX = itemPos.x;
var itemY = itemPos.y;
hero.moveXY(itemX, itemY);
}
}
}

problem is that you put var flag = hero.findFlag();
and then you put

if (flag) {
var itemPos = item.pos



}

not the flag, so basically you created a variable and you’re looking for the

  1. variable that you didn’t define
  2. The wrong variable

Also @juraj_pechac tried to explain you that you have to find flag’s pos, not the items pos.

Ok but its still saying that i am wrong…

I dont understand whats going on

I just figured it out. My code was right i just was’nt putting my flags on the x. Sorry about that I am new to this whole thing. Thankyou soo much for your help.

xD lol, I actually thought that you didn’t put the flags.