Check For The Presence of an Element With indexOf()

Check For The Presence of an Element With indexOf()
0

#1

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


#2

According to the instructions, your function must return a value of true or false. Currently, your function does not include a return statement of any kind.

The function is passed two arguments (arr and elem). You are supposed to be checking for the presence of elem in arr. The instructions show you exactly how you would write that. Also, understand that when using indexOf, the value returned from using it, is either the value -1 (means element was not found) or a number 0 up through the length of the array minus 1 (representing the first index where the element is found in the array).

Remember, that you can not just return the result of the indexOf, but instead figure out how you will use the result (-1 OR a value 0 or greater) to return a value of true or false.


#3

Hi, @bluebird008! I liked @RandellDawson’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

#4

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


#5

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.


#6

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


#7

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


#8

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;
}
}

#9

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


#10

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