// The indexOf() method returns the index within the calling String object of the first occurrence of the specified value, starting the search at fromIndex. Returns -1 if the value is not found.
// Case Sensitive
// STRING.indexOf()
let str = 'Winter is coming';
let position = str.indexOf('coming');
console.log(position); // 10
// Count occurences of a letter in a string
let str = 'Winter is coming';
let position = str.indexOf('i');
let count = 0;
while(position !== -1) {
count++;
position = str.indexOf("i", position + 1)
}
console.log(count); // 3
// ------------------------------------------------------------------ //
// ARRAY.indexOf() - array.indexOf(item, start)
let pieces = ['king', 'queen', 'rook', 'knight', 'bishop'];
console.log(pieces.indexOf('rook')); // 2
// Finding all the occurrences of an element
let indices = [];
let array = ['a', 'b', 'a', 'c', 'a', 'd'];
let element = 'a';
let ids = array.indexOf(element);
while (ids != -1) {
indices.push(ids);
ids = array.indexOf(element, ids + 1);
}
console.log(indices); // [0, 2, 4]