-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10_indexOf()ChecksForElement.js
More file actions
29 lines (24 loc) · 971 Bytes
/
10_indexOf()ChecksForElement.js
File metadata and controls
29 lines (24 loc) · 971 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/* Check for the Presence of an Element With
indexOf() */
/* indexOf() takes an element as an argument
and returns the first element index or -1 if the
element doesn't exist. */
let fruit = ['apples', 'pears', 'oranges', 'peaches', 'pears'];
console.log(fruit.indexOf('dates')); // -1
console.log(fruit.indexOf('oranges')); // 2
console.log(fruit.indexOf('pears')); // 1
/* 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 and 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. */
function quickCheck(arr, elem) {
// Only change code below this line
if (arr.indexOf(elem) > -1) {
return true
}
return false
// Only change code above this line
}
console.log(quickCheck(['squash', 'onions', 'shallots'], 'mushrooms'));