2018-10-10 22:03:03 +00:00
---
id: 587d7b7b367417b2b2512b14
2021-02-06 04:42:36 +00:00
title: Check For The Presence of an Element With indexOf()
2018-10-10 22:03:03 +00:00
challengeType: 1
2020-08-04 07:14:21 +00:00
forumTopicId: 301154
2021-01-13 02:31:00 +00:00
dashedName: check-for-the-presence-of-an-element-with-indexof
2018-10-10 22:03:03 +00:00
---
2020-12-16 07:37:30 +00:00
# --description--
2021-02-06 04:42:36 +00:00
Since arrays can be changed, or *mutated* , at any time, there's no guarantee about where a particular piece of data will be on a given array, or if that element even still exists. Luckily, JavaScript provides us with another built-in method, `indexOf()` , that allows us to quickly and easily check for the presence of an element on an array. `indexOf()` takes an element as a parameter, and when called, it returns the position, or index, of that element, or `-1` if the element does not exist on the array.
2020-12-16 07:37:30 +00:00
2021-02-06 04:42:36 +00:00
For example:
2020-08-04 07:14:21 +00:00
```js
let fruits = ['apples', 'pears', 'oranges', 'peaches', 'pears'];
2021-02-06 04:42:36 +00:00
fruits.indexOf('dates'); // returns -1
fruits.indexOf('oranges'); // returns 2
fruits.indexOf('pears'); // returns 1, the first index at which the element exists
2020-08-04 07:14:21 +00:00
```
2020-12-16 07:37:30 +00:00
# --instructions--
2018-10-10 22:03:03 +00:00
2021-02-06 04:42:36 +00:00
`indexOf()` can be incredibly useful for quickly checking for the presence of an element on an array. We have defined a function, `quickCheck` , that takes an array and an element as arguments. Modify the function using `indexOf()` so that it returns `true` if the passed element exists on the array, and `false` if it does not.
2018-10-10 22:03:03 +00:00
2020-12-16 07:37:30 +00:00
# --hints--
2018-10-10 22:03:03 +00:00
2021-02-06 04:42:36 +00:00
The `quickCheck` function should return a boolean (`true` or `false` ), not a string (`"true"` or `"false"` )
```js
assert.isBoolean(quickCheck(['squash', 'onions', 'shallots'], 'mushrooms'));
```
`quickCheck(["squash", "onions", "shallots"], "mushrooms")` should return `false`
2018-10-10 22:03:03 +00:00
```js
2020-12-16 07:37:30 +00:00
assert.strictEqual(
quickCheck(['squash', 'onions', 'shallots'], 'mushrooms'),
false
);
```
2018-10-10 22:03:03 +00:00
2021-02-06 04:42:36 +00:00
`quickCheck(["onions", "squash", "shallots"], "onions")` should return `true`
2018-10-10 22:03:03 +00:00
2020-12-16 07:37:30 +00:00
```js
assert.strictEqual(
quickCheck(['onions', 'squash', 'shallots'], 'onions'),
true
);
2018-10-10 22:03:03 +00:00
```
2021-02-06 04:42:36 +00:00
`quickCheck([3, 5, 9, 125, 45, 2], 125)` should return `true`
2018-10-10 22:03:03 +00:00
2020-12-16 07:37:30 +00:00
```js
assert.strictEqual(quickCheck([3, 5, 9, 125, 45, 2], 125), true);
```
2018-10-10 22:03:03 +00:00
2021-02-06 04:42:36 +00:00
`quickCheck([true, false, false], undefined)` should return `false`
2018-10-10 22:03:03 +00:00
2020-12-16 07:37:30 +00:00
```js
assert.strictEqual(quickCheck([true, false, false], undefined), false);
```
2018-10-10 22:03:03 +00:00
2021-02-06 04:42:36 +00:00
The `quickCheck` function should utilize the `indexOf()` method
2018-10-10 22:03:03 +00:00
```js
2020-12-16 07:37:30 +00:00
assert.notStrictEqual(quickCheck.toString().search(/\.indexOf\(/), -1);
2018-10-10 22:03:03 +00:00
```
2020-08-04 07:14:21 +00:00
2021-01-13 02:31:00 +00:00
# --seed--
## --seed-contents--
```js
function quickCheck(arr, elem) {
// Only change code below this line
// Only change code above this line
}
console.log(quickCheck(['squash', 'onions', 'shallots'], 'mushrooms'));
```
2020-12-16 07:37:30 +00:00
# --solutions--
2021-01-13 02:31:00 +00:00
```js
function quickCheck(arr, elem) {
return arr.indexOf(elem) >= 0;
}
```