Element With indexOf()

For this challenge, I don’t understand the solution. I understand the function indexOf and what it does. Not sure how is it comparing the argument array and the word mushroom by using >= 0? Can someone explain this to me?

function quickCheck(arr, elem) {
  if (arr.indexOf(elem) >= 0) {
    return true;
  return false;
console.log(quickCheck(["squash", "onions", "shallots"], "mushrooms"));

Do you know what the indexOf function returns when an item is not found in the array?

Yes it would return -1 if the element is not found or do not exist.

And -1 < 0

Right. but how is it returning false and comparing the Mushroom to the array of “squash”, “onions”, “shallots”?

In this function call

quickCheck(["squash", "onions", "shallots"], "mushrooms");

The argument arr is ["squash", "onions", "shallots"] and the argument elem is "mushrooms".
arr.indexOf(elem) >= 0 checks if elem is in arr.

If this syntax is unfamiliar to you, you are perhaps moving too quickly through the curriculum. You should understand how function arguments work by this point.

Were you able to solve this challenge without looking at the solution? If you were not, then you definitely are moving too quickly and should review earlier challenges.

Working through the challenges slowly and carefully will build your understanding faster than rushing through the challenges.

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