Check For The Presence of an Element With indexOf() [SOLVED]

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

#1

Tell us what’s happening:
What is the shortest solution for this challenge? I completed it using an if else statement but I feel like there is a much better solution for this.

Your code so far

js

function quickCheck(arr, elem) {
  // change code below this line
  let check = arr.indexOf(elem);
    if (check == 1 || check == 2 || check == 3){
        return true;
    }
    else {
        return false
    }

  // change code above this line
}

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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 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

Try this

function quickCheck(arr, elem) {
  return arr.indexOf(elem) === -1 ? false : true; 
}

#3

That is what i did
function quickCheck(arr, elem) {
// change code below this line
if ( arr.indexOf(elem) === -1 ){
return false;
} else {
return true;
}


#4

Also think about this:

You hardcoded the condition for index 1, 2, 3.
Your code will throw wrong results,
if the index is greater than 3.


#5

First, you don’t have to check for the presence of the actual index to see if it’s present.

A simple check > -1 is enough. This is because -1 is the value returned if the item is not present. So

let check = arr.indexOf(elem);
    if (check == 1 || check == 2 || check == 3){
        return true;
    }
    else {
        return false
    }

// can be written like this
// notice I don't need the else statement.
// this is because if the value is present
// the function will exit on return anyways
if (check > -1) return true
return false

Now, the simplest method, and what I would use to check if a value is present would not be the .indexOf method. This won’t pass the final test for obvious reasons.

Instead use .some(). This method will return true if at least one item in the array matches. That means it will only check until it finds the first match. From mdn:

The some() method tests whether at least one element in the array passes the test implemented by the provided function.

Note: This method returns false for any condition put on an empty array.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some

const quickCheck = (arr, elem) => arr.some(v => v === elem)


#6

why ternary operator isn’t working ?

I did this

let index = arr.indexOf(elem);

index > -1 ? true : false;

it isn’t working but if I use IF ELSE it’s working

// if(index > -1) {
// return true;
// } else{
// return false;
// }


#7

mmmh that’s really elegant


#8

Did you return ed a boolean? In your expression you’re just checking for a condition to be met and nothing to return.


#9

Hi guy look at my code :slight_smile:
return arr.indexOf(elem) > 0 ? true : false;


#10

Or

return arr.indexOf(elem) != -1;


#12

Here’s what I did, it seemed to work :slight_smile:

function quickCheck(arr, elem) {
// change code below this line

if (arr.indexOf(elem) > -1) {
return true;
} else {
return false;
}

// change code above this line
}

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


#13

you should use not equal to assignment when you know that only in one condition is throwing false.


#14

It would work if you put return in front of it, but you do not even need the ternary operator. You can simply write:

return index > -1;

which will evaluate to true or false and be returned.


#15

My way return arr.indexOf(elem) > 0 ? true : false instead of using -1

Hope this helps someone.


#16

" If greater than 0, would that not cause an issue if the indexed element held the first position in the array returning an index of 0? "
For clarity in reference to:

return (arr.indexOf(elem) > 0);


#17

If you a referring to @jmappa911’s post above, you are correct and glad you caught this error. With @jmappa911 return statement, the following call would incorrectly return false instead of true.

quickCheck([‘squash’, ‘onions’, ‘shallots’], ‘squash’); // should return true


#18

Thank you, yes I meant all the answers checking “greater than 0”. You could do something like
return ((arr.indexOf(elem) + 1) > 0;
Though I think checking against -1 is still probably easier for reading.
Checking against 0 satisfies the requirements of the exercise, though I think in application it could cause issues.


#19

here is my solution

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


#20

you should use >-1, index 0 is first element of array


#21

To sum it up:

If you like if clauses yuo can pass the test like this

 if (arr.indexOf(elem)=== -1 )  {
    return false;
  } else {
    return true;
  }

If you like more the elegant way, you can simplify this with a one liner

return arr.indexOf(elem) === -1 ? false : true;

Have fun and code on :slight_smile: