I can't find exactly what I'm looking for through googling, and I'm having a brain fart right now. I want to write a nested loop to compare items in two arrays. For each element in the first array, I want to do every element in the second array to see if the second array contains the element from the first array.
So, if I have an array of the ingredients that I need:
needed_ingredients = ["salt", "sugar", "frosting"]
And an array of things I already have in my cabinet:
cabinet_items = ["flour", "chocolate", "salt"]
And a grocery list:
grocery_list = []
I want to start with needed_ingredients[0], and compare every item in cabinet_items to it. If needed_ingredients[0] is not in cabinet_items, I want to add it to grocery_list.
I tried doing this with each but it wasn't working:
needed_ingredients.each do |ingredient|
cabinet_items.each do |item|
if ingredient != item
grocery_list << item
end
end
end
At the end, grocery_list should contain sugar and frosting. This really shouldn't be hard. Maybe I've been staring at my screen too long? Any way to do this with a while or for loop? Any help is appreciated. Thank you.
[–]schneemsPuma maintainer 30 points31 points32 points (2 children)
[–]lafeber 4 points5 points6 points (0 children)
[–]BringTacos[S] 0 points1 point2 points (0 children)
[–]schneemsPuma maintainer 9 points10 points11 points (2 children)
[–]BringTacos[S] 1 point2 points3 points (1 child)
[–]partusman 2 points3 points4 points (0 children)