- Home /
Check if array contains combinations of elements
I have an array that can hold any combination of, let's say, "Apple", "Orange" and "Water". I want to iterate through the array and see if the elements match a statement and then return a value, like this:
– If the array hold 1 "Apple" and 1 "Water", return "Apple Juice".
– 2 "Apple" and 1 "Water", also return "Apple Juice".
– 1 "Orange" and 1 "Water" return "Orange Juice".
– 1 "Apple", 1 "Orange" and 1 "Water" return "Multivitamin Juice".
– Any other combination should return "Nothing".
What is a good way to make this check? When I try this now with a normal if-statement, and say I have one of each item, it will start to check if I have 1 Apple and 1 Water, which is true (however I have an Orange also) and it will return Apple Juice and break out from the if, never checking if I also had the Orange. Is there a way to continue check for true statements further on even though one is already true.
simply don't return yet, but at the end of all the ifs