Useful Competitors JavaScript [Question/Help]

I just have a question with this level. I wrote the code two ways and failed once but passed the other time. I feel the code does the same thing both ways how ever I fail with the first code simply because it looks like my character does not move fast enough to get the coin since for w.e reason I stop moving at certain points with in the level.

Here is what the level asks you to do:

and here is what you are given at the start of the level to work with.


Here is my code from my first attempt:

while(true) {
    var enemy = hero.findNearestEnemy();
    if(enemy) {
        if(enemy.type != "peon") {
            hero.attack(enemy);
        }
    }
    var item = hero.findNearestItem();
    if(item) {
        // Gather the item only if the type is NOT equal to "poison".
        if(item.type != "poison") {
            hero.moveXY(item.pos.x, item.pos.y);
        }
    }
}

Here is a gif of the level running.

and here is the code from my second attempt that actually solved the level for me:


while(true) {
    var item = hero.findNearestItem();
    if(item && item.type != "poison") {
        hero.moveXY(item.pos.x, item.pos.y);
    var enemy = hero.findNearestEnemy();
    }
    if (enemy && enemy.type != "peon") {
        hero.attack(enemy);
    }   
}

Gif of the level with the passing code

If someone could please explain to me why one fails and why the other doesn’t that would be great. From what I understand in the second solution all I did was shorten up the code.

In the first solution the code is broken down like this if I am correct.

while(true) {
    var enemy = hero.findNearestEnemy();         // Set enemy Variable to find nearest enemy
    if(enemy) {                                  // If there is an enemy
        if(enemy.type != "peon") {               // If the enemy type is not equal to peon
            hero.attack(enemy);                  // Hero will attack the enemy
        }
    }
    var item = hero.findNearestItem();                  // Set item variable to find the nearest item
    if(item) {                                         // If there is an item
        if(item.type != "poison") {                   // If the item type is not poison
            hero.moveXY(item.pos.x, item.pos.y);     // The hero will move to the items position
        }
    }
}

Then in my working solution the code broken down works like this

while(true) {
    var item = hero.findNearestItem();            //I set the variable of item to find the nearest item
    if(item && item.type != "poison") {           //Then if there is an item and it is not poision
        hero.moveXY(item.pos.x, item.pos.y);      //Hero will move to that items position
    var enemy = hero.findNearestEnemy();          // Set variable to find nearest enemy
    }
    if (enemy && enemy.type != "peon") {          // If there is an enemy and the type isnt a peon
        hero.attack(enemy);                      //The hero will attack the enemy
    }   
}
1 Like

From the looks of it im pretty sure both of your solutions do the same thing except that you reversed the order.

It seems the hero cant execute another statement while a peon is on the map. Since you revered the order by looking for the items before the enemy its why you passed.

1st solution
Your hero would attack the enemy. Then it took time to get back to the items. By the time your reached the item another peon already spawned. You had to stop and wait then you grabbed another item and then attacked the enemy.

2nd solution
You were looking for the items first and once moveXY is executed nothing else will execute until you reach the moveXY destination. You also changed the solution up a bit by only giving the enemy variable a value only if their was an item.

I dont think you should get caught up in to much of the details. Just know how the functions work. You cant execute any more statements until your at the moveXY destination. Im mentioning this because when you get the right boots you will have a method called move which is a game changer because you can keep on executing code while moving.

Also know your gear had a part in this to. If you could see the whole map then you might of lost no matter what solution you used. You could restrict your distance viewing by saying only if the enemy is in a certain distance but you dont have to. You only failed on the 1st solution because you were going back and forth more often.

Im pretty sure your 1st solution would work if you were faster.

Id also like to say im extremly impressed with your documentation. I never see such elegance in a post.

Ah so its my items! I wasn’t sure if that was the case! There’s so many items in the shop I don’t know what to buy and when. And thanks I take a lot of time in my questions so the reader can fully understand my problem and what I understand and what I don’t. Sometimes I can catch my own mistake when I go over every detail like this as well.

Quick question how did you do a video screen shot? I can only do picture screen shots.

I use a program called ScreenToGif. You can get it here.

Its very simple to use, you drag the box over the area u want to recrod hit f7 or the record button to record it then stop, then it will open up a menu where you can edit the gif, give it a file location and name then save it and it will render the gif. Than I use giphy to upload them since they tend to be to big to post here directly.

1 Like

That’s awesome!
(20 Characters)

Its like 5 months later and they added shooting people. So what do I do?

They added it in the beginning.