Check For The Presence of an Element With indexOf() using ternary operator

I have already solved this using an if…else statement, but i tried using a one line ternary operator but it isn’t working… am i doing something wrong?:

Your code so far

function quickCheck(arr, elem) {
  // change code below this line
  arr.indexOf(elem) > 0 ? true : false;
  // 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 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36.

Link to the challenge:

With if...else statement, you probably wrote return true/false, whereas your function now doesn’t return a thing. You just check for the value and silently exit the function. So, in order for test console.log call to actually log a value, you must return it :slight_smile:

1 Like

Just to expand on what @vytautas-pilk said, if/else is a statement, a ternary is an expression.

if () {
} else {

Doesn’t evaluate to a value, it’s the expressions inside the blocks that do, so you return those

if (condition) {
  return thing1;
} else {
  return thing2;

A ternary does evaluate to a value:

(condition) ? thing1 : thing2

Is a value that is either thing1 or thing2, so you have to return the whole thing:

return (condition) ? thing1 : thing2;

BTW, you are also skipping the first element.

This should return true, but it doesn’t:

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