Decoy Drill Problem Javascript


#1

I am having trouble with this level. Everything works, aside from the fact that when four decoys are built, the person just keeps going and getting more decoys. Although I have created four decoys, the person never breaks out of the loop. Please help. what’s wrong with my code?

// We are field testing a new battle unit: the decoy.
// Build 4 decoys, then report the total to Naria.
// Each decoy costs 25 gold. Use the Quartz Sense Stone
// to know when you have more than 25 gold with this.gold.
// Keep a count of decoys you built as you go along.
// Break out of the loop when you have built 4.
loop {
    var decoysBuilt = 0;
    var gold = this.gold;
    var coin = this.findNearestItem();
    if (coin){
        cx = coin.pos.x;
        cy = coin.pos.y;
        this.moveXY(cx, cy);
    }
    if (gold >= 25){
        this.buildXY("decoy", cx, cy); 
        decoysBuilt = decoysBuilt + 1;
    }
    if (decoysBuilt === 4){
        break;  
    }
}
this.say("Done building decoys!");
// Go to Naria and say how many decoys you built.
this.moveXY(14, 36);
this.say("decoysBuilt");

#3

i think u do not need to use a “loop:” in this level or earlier the “while-loop” should be implemented.

if you dont know what i mean try sth like:

while(decoysBuild<=x){…}


#4

does === work for numbers (I know it does something I just can’t remember what)
but == should work.

and you need to remove the quotes from

this.say("decoysBuilt");

so you say “4” instead of “decoysBuilt”


#5

Yes, it works for numbers. In ES/JS, If the any one of the two operands are objects, then === checks to see if they are the same object. == simply checks the value/valueOf() the object. There are a few other technicalities, but that’s the main one and can produce wildly different answers depending upon the variables/function returns involved.


#6

that was the only weird thing I saw.

BUT now that that is out of the way…

I can see that your decoysBuilt = 0 is inside the loop


#7

It still is not working. I used the while(decoysBuilt == 4); I also removed the quotation marks from this.say(decoysBuilt);

This is my new code:

// Build 4 decoys, then report the total to Naria.
// Each decoy costs 25 gold. Use the Quartz Sense Stone
// to know when you have more than 25 gold with this.gold.
// Keep a count of decoys you built as you go along.
// Break out of the loop when you have built 4.
loop{
    var decoysBuilt = decoysBuilt;
    var gold = this.gold;
    var coin = this.findNearestItem();
    if (coin){
        cx = coin.pos.x;
        cy = coin.pos.y;
        this.moveXY(cx, cy);
    }
    if (gold >= 25){
        this.buildXY("decoy", cx, cy); 
        decoysBuilt = decoysBuilt + 1;
    }
    while(decoysBuilt >= 4){
        break;
    }
}
this.say("Done building decoys!");
// Go to Naria and say how many decoys you built.
this.moveXY(14, 36);
this.say(decoysBuilt);```

IT STILL DOESN"T WORK! PLEASE HELP! (I am not yelling or mad at you guys I just felt like bolding that.)

#8

Whoops, I didn’t mean to withdraw this.


#9

You need to set “decoysBuilt” to zero BEFORE entering the loop

should be


#10

should i still have it after the loop as well?


#11

No. You only want to give it an initial value before entering the loop and then increase that value (as you do) whenever you have built a new decoy.

You may also want to change

to

The while-loop will do the trick as it is, but isn’t necessary since the condition will only be true one single time.


#12

i think you should tell him how manny you have built… so "ive build " + decoisBuild + “decois”


#13

this part is good, I actually used that type of statement many times last night. However, this code did not work for me:

change that, to this:
if (decoysBuilt >= 4)


#14
  1. Always is better to use x>= limit than x== limit
    If your code allows 2 decoys to be built before testing, then the number of decoys can go from 3 to 5 before you test it again

  2. Use 2 signs == when you are comparing to non-zero and
    three signs === when you are comparing to 0. The double == signifies that you are making a comparison and not an assignment.

Additionally, when comparing values of different types a conversion will be made.
0 is a special value and many different types of data can be converted to 0 if they are "false"
Use ===0 to disable these conversions and specify than you really want to see if a value is exactly 0.

if(a ==4) ...
if(a===0) ....
```