Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

Golden Mirage : trying to sort out coins (Javascript)

I am currently trying to do the golden mirage level on the desert. I’ve done similar levels like ice hunter, the spy among us, reindeer tender, etc. But my code wont click. It’ll get two fake coins, a streak of real ones, and fake coins again. What’s wrong with my code?

Here’s my code:

// Collect 10 real coins.
function checkCoins (items) {
var fakeCoins =[];
var trueCoin = null;
for(var i = 0; i < items.length; i++) {
    var coin1 = items[i];
    for (var j = 0; j < items.length; j++) {
        var coin2 = items[j];
        if (i == j) {
            continue;
        }
        if (coin1.value == coin2.value) {
            fakeCoins.push(coin1);
             fakeCoins.push(coin2);
             
            break;
        }
        if (coin1.value != coin2.value) {
            trueCoin = coin1;
            break;
        }
    }
}
return trueCoin;
}
while(true) {
    var items = hero.findItems();
    var real = checkCoins(items);
    if (real) {
        hero.move(real.pos);
    }
}

Some explanation on how to fix it would be nice. Thanks in advance.

You’re indentation is a bit off. Next time could you post it with all the correct indents because indents are very important, especially in this level.
I’ll try and help anyway;

Even if this condition is true: “coin1.value does not equal coin2.value”, does it mean it’s not a fake coin? Couldn’t this line be run before you had a chance to loop through all the “j” coins (the coins in your second for loop) and compare them with the first loop? Meaning even if you return the coin as real it could still be fake. Maybe make an array for real coins and only check whether the coin is fake or not after you’ve run the second for loop; and if it isn’t in the fake coins array after the second loop you could append it.
I hope this helps
Danny

Can you please iterate exactly what you mean? Is it something like this?

if (coin1.value != coin2.value) {
                realCoins.push(coin1);
                break;
            }
//After iterating all coins
for (var l = 0; l < realCoins.length; l++){
var real1 = realCoins[l];
if (realCoins.length > 2) { 
for (var t = 0; t < realCoins.length; t++){
var real2 = realCoins[t];
if (l == t){
continue;
}
if(real1.value == real2.value){
fakeCoins.push(real1)
fakeCoins.push(real2)
break;
}
} else {
trueCoin = real1
}
}

Am i doing this Correctly, or am i just making it worse?
P.S : sorry for the formatting. i can’t use tab if i’m just writing

Also, does the append code mean the same as the push code in JS? because there is no append function on my code

I’m sorry, but your code still isn’t formatted and it does make a difference.
Read this topic to explain it more:
[Essentials] How To Post/Format Your Code Correctly
Danny
P.S. Append is the same as push(). And from what I can see in your code it was better before, I’ll explain it more if you haven’t solved it.

Thanks for the help, it’s solved.