Crafting tools confusion

Hello, i’m making the crafting weapons code and i have some mental confusion on a part of code, when i click the tool i want to forge have to check if all the resources needed are in inventory and if they are have to delete that resources. I had no problem since the resources was all with differents names but if i need 3 irons it became more complicated.
Here is the code:

               self.recipe=item.id;
                for (var i=0;i<self.player.inventory.length;i++) {
                    if (self.player.inventory[i].name===item.ingr1) {
                        var i1=i;
                    } 
                    if (item.ingr2 && self.player.inventory[i].name===item.ingr2) {
                        var i2=i;
                    } 
                    if (item.ingr3 && self.player.inventory[i].name===item.ingr3) {
                        var i3=i;
                    } 
                }
                if (!i1) {app.root.findByName('Root').script.Output.write("You don't have "+ item.ingr1); return;}
                if (!i2 && item.ingr2) {app.root.findByName('Root').script.Output.write("You don't have "+ item.ingr2); return;}
                if (!i3 && item.ingr3) {app.root.findByName('Root').script.Output.write("You don't have "+ item.ingr3); return;}
                app.root.findByName('Root').script.Output.write("You start forging "+ item.name);

With the code as it is if i have just 1 iron it pass the check…also if 3 irons are needed.
And after that i need to remove the resources from inventory so i need to sort i1-i2-i3 from higher to lower and splice them from inventory…i have some problem sorting them

I haven’t tested this but I think something like this should work

var i1 = null;
var i2 = null;
var i3 = null;
var needed = 1;
if (item.ingr2) needed++;
if (item.ingr3) needed++;

for (var i = 0; i < inventory.length; i++) {
    if (i1 === null && inventory[i].name === item.ingr1) {
        i1 = i;
        needed--;
    } else if (item.ingr2 && i2 === null && inventory[i].name === item.ingr2) {
        i2 = i;
        needed--;
    } else if (item.ingr3 && i3 === null && inventory[i].name === item.ingr3) {
        i3 = i;
        needed--;
    }

    if (needed === 0) {
        break;
    }
}

And then you would check if i1, i2 or i3 are null (don’t do if (! i1) as this will fail if i1 is 0 which is a valid index.

uhm i see…the reverse counter is indeed a nice idea, it needs some adjustment coz not all the resources will be the same but should work fine. Tnx @vaios, will let you know later when i try it