Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialElijah Franklin
882 PointsI need help simplifying this code.
So I have an if statement that executes based on if certain integers meet the proper conditions, but I don't want the order of which to matter. Hopefully the code explains itself better than I can:
if (item1 == 7 && item2 == 8 || item1 == 8 && item2 == 7)
{
successful = true;
Debug.Log("Works");
}
I'm looking for a solution now so I don't have to manually code it in the future where I'll have item1-6 and every possible order the integers could appear in...
There's probably a simple solution to this, but It has exceeded me.
1 Answer
Steven Parker
231,236 PointsAs I understand the task, you want to check a quantity of items to be sure at least one of every value from a unique set is present in the items. So if you put the items into an array instead of separate variables, you could do this:
let choices = [2, 3, 6, 9]; // these must all be present in "items" in any order
successful = choices.reduce((a,x) => a && items.includes(x), true);
if (successful) {
// ...
This should work for any number of items & choices.
Elijah Franklin
882 PointsElijah Franklin
882 PointsThank you for your help!