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");
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.
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.)
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
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.