Check For The Presence of an Element With index

Check For The Presence of an Element With index
0

#1

Tell us what’s happening:
I followed the solution for this but i want to further my grasp of the solution, can someone explain the syntax of the solution in detail as i have a general idea as to what’s going on but any additonal information would help.

Your code so far


function quickCheck(arr, elem) {
  // change code below this line
return arr.indexOf(elem)=== -1 ? false : 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/71.0.3578.98 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-data-structures/check-for-the-presence-of-an-element-with-indexof


#2

You mean the ternary Operator?

It goes like this
(Evaluate this) ? (If it is true execute this) : (else execute this)
In this case whatever is executed it is returned

Meaning "if elem has an index of -1 (Not included in the array), return false, else return true"

If you are not familiar with the ternary Operator you can solve this with an if … else statement


#3

Understood thank you for the response i did some other research on this as well but you response was helpful.


#4

It could also have been more simply just like this. It returns true if indexOf return a number different than -1 and false otherwise


#5

return arr.indexOf(elem) >= 0;

Is even shorter.