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:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-data-structures/check-for-the-presence-of-an-element-with-indexof
Hi,
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 
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;
2 Likes
BTW, you are also skipping the first element.
This should return true, but it doesn’t:
console.log(quickCheck(['squash', 'onions', 'shallots'], 'squash'));
false
2 Likes