I fixed the issue from last night where some materials never reached a high enough priority to be mined. As I had planned, I broke miningAlgorithm
into two pieces: one that calculates priority and chooses what material to mine, and another, chooseMine
, which chooses the best mine where you can find that material.
Unfortunately, that caused another problem, but I’ll have to get back to that. The good thing is that I learned more about using function expressions.
I made a function expression let priority = function () {/* yadda yadda */}
. That checked to see if we’re running out of available mines (if (i >= availableMines - minableNeeds)
) and we loop through each of the minable needs to make sure there’s at least one mine for each need. If not, the we return the need we’re looking at. If there’s at least one mine, we return the top priority as per miningAlgorithm
. If we’re not in the last few available mines, we also return the top priority per the algorithm.
Once I have returned priority
I can call chooseMine(priority(), i)
. I was not expecting to have to use the parentheses in the parameters, but now that I’m thinking of it, that makes sense. It means when it finds that it needs priority
, it has to call it as a function. Otherwise, it passes in the text of the function, which helps no one.
And that works great. Except when there are no mines left that contain the designated priority.
Previously, I had been looping through needsList
, which was recently sorted by priority. If I ran out of mines for one material, it would automatically move on to the next highest priority.
But now that I’m determining the top priority, I though there would be no need to loop through needsList
yet again. I got rid of the loop, and I was happy for slightly cleaner code. Alas.
Instead, of returning priority
, I’m going to have to move the position of the desired material in needsList
. Then I can loop through it, just as I had been before.
To do this, I think I can unshift()
to make room at index 0, copy the desired material, and splice()
the old one out. Luckily, I want the priority at the beginning of the array, so I don’t have to go and do something like this.
That also means I probably won’t need a function expression. Oh well.