I’ve been trying this level for a bit, and I followed all the instructions, but it still says that I have a bug in my code! Please can you help me fix it? Here is my code:
// You need exactly 104 gold.
var less = "Nimis";
var more = "Non satis";
var requiredGold = 104;
// This function calculates the sum of all coin values.
function sumCoinValues(coins) {
var coinIndex = 0;
var totalValue = 0;
// Iterate all coins.
while (coinIndex < coins.length) {
totalValue += coins[coinIndex].value;
coinIndex++;
}
return totalValue;
}
function collectAllCoins() {
var item = hero.findNearest(hero.findItems());
while (item) {
hero.moveXY(item.pos.x, item.pos.y);
item = hero.findNearest(hero.findItems());
}
}
while (true) {
var items = hero.findItems();
var item = hero.findNearestItem();
// Get the total value of coins.
var goldAmount = sumCoinValues(items);
// If there are coins, then goldAmount isn't zero.
if (goldAmount !== 0) {
// If goldAmount is less than requiredGold
if (goldAmount < requiredGold) {
hero.say("Non satis");
} else if (goldAmount > requiredGold) {
hero.say("Nimis");
if (goldAmount = requiredGold) {
//it says that it expected a
//"conditional expression"
//and instead found an assignment
hero.findNearestItem();
hero.move(item.pos);
}
// Then say "Non satis".
// If goldAmount is greater than requiredGold
// Then say "Nimis".
// If goldAmount is exactly equal to requiredGold
// Then collect all coins:
}
}
}
Hello and welcome to codecombat discourse, @HeroA (at least I think you’re new)! This is a cozy forum where you can share ideas, share fan art, get assistance for code, etc! Before you proceed, we hope that you review this topic, which shows all essentials of this board! Thanks!
anyway, for != you only need one equal sign (even in if statements) and you have to use the collectAllCoins() function to collect all coins.
I did that, but my code still doesn’t work. Is there anything else that I should fix? Here it is:
// You need exactly 104 gold.
var less = "Nimis";
var more = "Non satis";
var requiredGold = 104;
// This function calculates the sum of all coin values.
function sumCoinValues(coins) {
var coinIndex = 0;
var totalValue = 0;
// Iterate all coins.
while (coinIndex < coins.length) {
totalValue += coins[coinIndex].value;
coinIndex++;
}
return totalValue;
}
function collectAllCoins() {
var item = hero.findNearest(hero.findItems());
while (item) {
hero.moveXY(item.pos.x, item.pos.y);
item = hero.findNearest(hero.findItems());
}
}
while (true) {
var items = hero.findItems();
var item = hero.findNearestItem();
// Get the total value of coins.
var goldAmount = sumCoinValues(items);
// If there are coins, then goldAmount isn't zero.
if (goldAmount != 0) {
// If goldAmount is less than requiredGold
if (goldAmount < requiredGold) {
hero.say("Non satis");
} else if (goldAmount > requiredGold) {
hero.say("Nimis");
if (goldAmount = requiredGold) {
//it says that it expected a
//"conditional expression"
//and instead found an assignment
collectAllCoins();
}
// Then say "Non satis".
// If goldAmount is greater than requiredGold
// Then say "Nimis".
// If goldAmount is exactly equal to requiredGold
// Then collect all coins:
}
}
}
What you need to do is check if goldAmount is less than requiredGold. If it is, then use hero.say() and say “Non Satis”. Then you need to check if goldAmount is greater than requiredGold. If it is, then use hero.say() and say “Nimis”. If goldAmount equals requiredGold, then use your collectAllCoins() function to collect all the coins.