Check For The Presence of an Element With indexOf() -- help

Tell us what’s happening:

I understand there are various ways to do something in JS but I feel that I may have complicated things, is there a more straightforward way I should have approached this problem? Or is my way the way you would have approached the problem?

Your code so far

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

// change code here to test different cases:
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/67.0.3396.99 Safari/537.36.

Link to the challenge:

It’s correct. If you really want to shorten it even further:

function quickCheck(arr, elem) {

  return arr.indexOf(elem) > -1;


Shorter to use includes method. :grinning:

1 Like

Although, the purpose of the exercise was to use indexof so that may not pass the test, but is certainly easier.

That is true. I got carried away.

Perhaps, but it’s still good to remind newer coders (like me) of the better functions’ existences.

“Newer” coder. Don’t make me feel so bad.

Just means we still have a lot to learn.