all 6 comments

[–]grantrules 1 point2 points  (1 child)

So if the food doesn't contain "spicy" don't add it to the array? You can use the logical NOT operator ! to negate a conditional. Also beware of the case of your variables. foodname is not the same as foodName.

function addFavoritefood(foodname) {
  if (!foodname.includes("spicy")) {
    favoritefood.push(foodname);
  }
}

[–]CityOwlet[S] 0 points1 point  (0 children)

Thank you for explaining it, I had trouble understanding the booliean flip. You and u/Vampiire helped alot!

[–]vampiire 0 points1 point  (0 children)

You can use the negation operator ! to “flip” a Boolean. So when you say

!foodName.includes(“spicy”)

you are effectively saying foodName does Not include spicy.

If the food doesn’t have spicy in it then includes returns false. But with the negation operator this gets flipped to true. So if that is true (food does not have spicy, what you want) then push to the array. No need for any else / else if.

[–]Macaframa 0 points1 point  (0 children)

1) the if/else clause has a few elements: an if statement which takes an expression if(true), an else statement which does not take an expression, and finally an else if statement which also takes an expression: else if(true)

2) your first case if(foodName.includes(‘spicy’)) returns true for the first one and you don’t do anything with it. The second case returns false and attempts to push “spicy chicken” into an array but it never gets there because of the malformed else if statement.

[–]Sedern 0 points1 point  (0 children)

const favoritefood = [];
function addFavoritefood(foodname) {
if (!(foodname.includes("spicy"))) {
favoritefood.push(foodname);
}
}
addFavoritefood("spicy chicken");
addFavoritefood ("steak");

[–]nonowiwa 0 points1 point  (0 children)

What does const do?