Explanation of challenge in the image.
I am at a loss as to where my error is and how to fix it. Any help would be wonderful. I am very much a beginner. I get this error : TypeError: allergies.forEach is not a function. Examples of ‘order’ inputs : ‘Classic,-strawberry’ OR “Classic” OR ‘Just Desserts,-ice cream,-peanut’. The code works for just “Classic” but the more complex inputs do not work and throw up the error
const ingredientsList = {
Classic: [ "strawberry", "banana", "pineapple", "mango", "peach", "honey", "ice", "yogurt" ],
"Forest Berry": [ "strawberry", "raspberry", "blueberry", "honey", "ice", "yogurt" ],
Freezie: [ "blackberry", "blueberry", "black currant", "grape juice", "frozen yogurt" ],
Greenie: [ "green apple", "kiwi", "lime", "avocado", "spinach", "ice", "apple juice" ],
"Vegan Delite": [ "strawberry", "passion fruit", "pineapple", "mango", "peach", "ice", "soy milk" ],
"Just Desserts": [ "banana", "ice cream", "chocolate", "peanut", "cherry" ]
} ;
function ingredients(order) {
//check if order arguement includes ingredients to be exculded
if (order.includes("-")) {
return ingredientListSorter(order);
} else {
return ingredientsList[order].sort().toString();
}
}
function ingredientListSorter(order) {
const orderArray = order.split(",-") //split order arguement into its parts
const smoothieIngredients = ingredientsList[orderArray[0]] // select the correct smoothie ingredients arr from list
const allergies = orderArray.shift() // select ingredients to remove (deleting first element of array)
// iterate removing ingredients from the smoothie ingredient list
allergies.forEach((ingredient) => {
const index = smoothieIngredients.indexOf(ingredient)
if (index >= 0) smoothieIngredients.splice(index, 1)
});
// return updated smoothie ingredient list, alphabetised.
return smoothieIngredients.sort().toString();
}