Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to iterate through a dictionary to get items that make up a certain price?

Asked by
RiotGUI 15
3 years ago
Edited 3 years ago

I apologize if my question didn't make enough sense, I'm kind of still learning.

local UserItems = {
    ['Banana'] = 20429;
    ['Apple'] = 50000;
    ['Watermelon'] = 250000;
    ['Pineapple'] = 30000;
}

Let's say that a user has these items above, how do I iterate through the dictionary and get the items that costs, let's say, 300K? (In that case, it should pick the Apple and Watermelon to make up the 300K)

0
Are you trying to find the elements which, when summed together equal to 250 thousand? Or the single element that costs 250 thousand? radiant_Light203 1166 — 3y
0
Yes exactly, when summed up together they equal 250,000. RiotGUI 15 — 3y
0
And 250,000 is not constant, it's just an example, I want it to get the suitable items that sum up to a certain number, like if my input is 330,000 it should pick watermelon, apple, and pineapple. Also sorry for my misleading question i should've said 300K not 250K. RiotGUI 15 — 3y
0
Fixed my question. RiotGUI 15 — 3y
View all comments (2 more)
0
This is a pretty tough question, since there isn't a built-in method to do exactly what you're asking for and mostly likely requires making an alogrithm of your own. I'll try spending some time on this question and if I can, I'll give you an answer radiant_Light203 1166 — 3y
0
Your help in this matter is really appreciated, thank you so much. RiotGUI 15 — 3y

Answer this question