Flawless Pairs Javascript help (Solved)

// Collect 4 pairs of gems.
// Each pair must contain equal valued gems.
// This function returns two items with the same value.
function findValuePair(items) {
    // Check each possible pair in the array.
    // Iterate indexes 'i' from 0 to the last one.
    for (var i = 0; i < items.length; i++) {
        var itemI = items[i];
        // Iterate indexes 'j' from 0 to the last.
        for (var j = 0; j < items.length; j++) {
            // If it's the same element, then skip it.
            if (i == j) {
                continue;
            }
            var itemJ = items[j];
            // If we found a pair with two equal gems, then return them.
            if (itemI.value === itemJ.value) {
                return [
                    itemI,
                    itemJ
                ];
            }
        }
    }
    // Return an empty array if no pair exists.
    return null;
}
while (true) {
    var gems = hero.findItems();
    var gemPair = findValuePair(gems);
    // If the gemPair exists, collect the gems!
    if (gemPair) {
        var gemA = gemPair[0];
        var gemB = gemPair[1];
        // Move to the first gem.
        hero.move(gemA.pos);
        // Return to get the haste from the wizard.
        hero.moveXY(40, 44);
        // Then move to the second gem.
        hero.move(gemB.pos);
        // Return to get the haste from the wizard.
        hero.moveXY(40, 44);
    }
}

The hero does not go anywhere.
Thanks

Hello, could you please send me the link to the level?

Here is the link.

Thanks, I’ll take a look!

1 Like

Hmm, strange, my code is identical to yours(nearly).
Try changing this line

 hero.move(gemA.pos);

to this

 hero.moveXY(gemA.pos.x, gemB.pos.y);

Same with B stone.
In other your code is same as mine(I passed this level)

1 Like

Yes, i checked this code and it works with the change above.Make it and you will pass the level!

1 Like

It was that simple. I passed the level!
Thank you

You are welcome!Congratilations! :partying_face: :partying_face: :partying_face:

1 Like

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.