Coding challenge help! > TypeError: allergies.forEach is not a function

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();
}

Did you log out allergies to find out what it is? That would have been my first instinct. It is saying that allergies doesn’t have the method forEach, which means that it is not an array.

One possible source of confusion is this line:

const allergies = orderArray.shift() 

The shift method does not return the array, but the shifted element.

thank you! you are totally correct. I ended up using

const allergies = orderArray.slice(1)
1 Like

I suppose you could have kept the shift and just used the modified orderedArray after that.

But they both work and they both seem like valid approaches. Good work.

I have encountered a new problem. If your order looks like (includes something not in the ingredient list for Classic)

'Classic,-kiwi'

then the output is

banana,honey,ice,peach,pineapple,yogurt

but it should be

banana,honey,ice,peach,pineapple,strawberry,yogurt

the strawberry ingredient is being removed when it should not. (it is index(0) of the unsorted array)

any ideas why this is happening?

I don’t know, we’d have to see your code. It works for me:

console.log(ingredients('Classic,-kiwi'));
// banana,honey,ice,mango,peach,pineapple,strawberry,yogurt

You can see it in a pen here.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.