Nelu
0
Q:

index()

/"returns the index, of that element, or -1 if the element does not exist."/

let fruits = ['apples', 'pears', 'oranges', 'peaches', 'pears'];

fruits.indexOf('dates'); // returns -1
fruits.indexOf('oranges'); // returns 2
fruits.indexOf('pears'); // returns 1, first index at which the element exists
4

fruits = ['apple', 'banana', 'cherry']
print(fruits.index("cherry"))
#--> 2
4
my_string = "Hello World!"

my_string.index("l") # outputs 2
# this method only outputs the index of the first "l" value in the string/list
7
# vowels list
vowels = ['a', 'e', 'i', 'o', 'i', 'u']

# index of 'e' in vowels
index = vowels.index('e')
print('The index of e:', index)

# element 'i' is searched
# index of the first 'i' is returned
index = vowels.index('i')

print('The index of i:', index)
0

New to Communities?

Join the community