Basic Data Structures: Check For The Presence of an Element With indexOf()

Tell us what’s happening:

hello why this is giving me error and not completing the challenge.
thanks !

Your code so far
function quickCheck(arr, elem) {
// change code below this line
let array = arr.indexOf(elem);
if (array = elem){
return true;
} else {
return false;
};

// change code above this line
}

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

// running tests
quickCheck([“squash”, “onions”, “shallots”], “mushrooms”) should return false
// tests completed
// console output

true


function quickCheck(arr, elem) {
// change code below this line
 let array = arr.indexOf(elem);
 if (array = elem){
   return true;
 } else {
   return false;
 };
 
// change code above this line
}

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

//console results :"

// running tests
quickCheck(["squash", "onions", "shallots"], "mushrooms") should return false
// tests completed
// console output

true


Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36.

Challenge: Check For The Presence of an Element With indexOf()

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

  • let array = arr.indexOf(elem); This sets array to a number.
  • Remember that = is the assignment operator. It doesn’t check for equality.

thanks , i didn’t understand it first but after thinking of it , and based on your comment,

indexOf > to show the index of an element [0,1,2,3,4]
if indexOf something is not in the array it will return -1
so if index of something is in the array it should be from 0 to a number based on the array length.

hope that helps who is stock in this in future :slight_smile:

1 Like