Check For The Presence of an Element With indexOf() , Data structures

Tell us what’s happening:
My code not producing what’s expected
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/88.0.4324.182 Safari/537.36.

Challenge: Check For The Presence of an Element With indexOf()

Link to the challenge:

insexOf() […] returns the position, or index, of that element, or -1 if the element does not exist on the array.

the issue here is that when the element is not present the method returns -1 which is truthy, and so you return true, when the element is present but at index 0 the method returns 0 which is falsy and you return false

you can’t just put the method in the if condition

Thanks for the help but for sure I have failed to go through this challenge after several trials

what does indexOf return if the element is present? what does indexOf return if the element is not present?

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