Basic Data Structures - Check For The Presence of an Element With indexOf()


I used if-else to check the index of array elements, and it does work, but it is not passing all the test case.
Please check my code and tell me where I need to improve.

Your code so far

function quickCheck(arr, elem) {
  // Only change code below this line
  if(arr.indexOf(elem)){
    return true;
  }else{
    return false;
  }
  // Only change code above this line
}

console.log(quickCheck(['squash', 'onions', 'shallots'], 'mushrooms'));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36

Challenge: Basic Data Structures - Check For The Presence of an Element With indexOf()

Link to the challenge:

indexOf returns a number. Either the index position of the element or -1 if it doesn’t find the element. Its return value is not suited for boolean coercion. Check the return value is >= 0


If you wanted to use the logic you have you would use includes which returns true or false.

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