Check For The Presence of an Element With indexOf()

Tell us what’s happening:

What to do now ?
Your code so far


function quickCheck(arr, elem) {
  // change code below this line
quickCheck.indexOf('onions')
  // 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.87 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

1 Like

Hi, @bluebird008! I liked @camperextraordinaire’s answer, but I’m going to take a stab at it too:

indexOf is a method that you can use on Arrays or Array-types (ex. Strings). The purpose of the method is to search your array for something inside of it.

If it successfully finds an item, it will return the first index where it is found so 0, 1, 2, etc.
If it does not find the item, it will return -1.

['apples', 'oranges', 'bananas'].indexOf('apples'); // 0
['apples', 'oranges', 'bananas'].indexOf('oranges'); // 1
['apples', 'oranges', 'bananas'].indexOf('strawberries'); // -1, not found

I get the idea , But i can’t understand how to do it . So after reading all of your posts , i think it would be like this

arr.indexOf('elem');
return arr;

But it’s not , I am sorry , i still couldn’t pass

As your code stands, you are returning the index of the element, or -1 if the element doesn’t exist.

The challenge actually asks you to return true if the element exists, and false if it doesn’t exist.

Your functional logic is correct, you just need to modify the return to satisfy the challenge requirements.

I am also stuck here:: Please help.
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’], ‘onion’));

I’m having the exact same issues, can someone please help.
This is my code:

function quickCheck(arr, elem) {
// change code below this line
if (arr.indexOf(’ elem ') <= 0){
return true;
}
else{
return false;
}

// change code above this line
}

// change code here to test different cases:
console.log(quickCheck([‘squash’, ‘onions’, ‘shallots’], ‘mushrooms’));

hey you used quotes around parameter elem

indexOf - returns -1 if the value not found in the array , if value found in the array it returns the position of that item
so your condition <= 0 only true for item at index 0

try like this

function quickCheck(arr, elem) {
 if (arr.indexOf(elem )!== -1){
return true;
}
else{
return false;
}
}
1 Like

Thanks it worked!! I initially did it without the quotes but it wasn’t working, thanks to you I now know why.

Hello! Here is my solution:

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'));