const colors = ["red", "green", "blue"];

/*
 * Note:
 * Array indices start at 0.
 * The first element has index 0, the second element has index 1.
*/

// Returns the index of the first found occurrence of "red" in the colors array
const red = colors.indexOf("red");
console.log(red); // Output: 0

// Returns -1 because "yellow" is not found in the colors array
const yellow = colors.indexOf("yellow");
console.log(yellow); // Output: -1

const blue = colors.indexOf("blue", 1);
// The second argument specifies the index to start the search from.
// This argument is optional; if omitted, the search starts from index 0.
// Since the argument is 1, the search starts from the second element.
// The element "blue" is found at index 2, which is the first occurrence from index 1.

console.log(blue); // Output: 2
arr.indexOf(searchElement[, fromIndex])
/*
 * If the element appears multiple times in the array,
 * the index of the first found occurrence is returned.
*/
const colors = ["red", "green", "green", "blue"];

// Returns the index of the first found "green" in the colors array
const green = colors.indexOf("green");

// There are two "green" elements in the colors array
// at indices 1 and 2.
// However, the function returns the index of the first found element, which is 1.
console.log(green); // Output: 1


/*
 * arr.indexOf(searchElement[, fromIndex])
 *
 * If the optional fromIndex argument is negative,
 * it counts from the end of the array.
 * However, the search is still performed from left to right.
*/
const fruits = ["apple", "bannana", "orange", "mango"];
const index = fruits.indexOf("mango", -2);

console.log(index); // Output: 3
// The fromIndex value is -2.
// Counting from the end of the array, the search starts at the second-to-last element, "orange".
// However, the search is still performed from left to right.
// The element "mango" is located to the right of "orange", 
// so the index 3 is returned in the fruits array.
const arr = [1, 2, NaN, 4, 5];
console.log(arr.indexOf(NaN)); // Output: -1
const colors = ["red", "green", "blue"];
const red = colors.indexOf("Red");

// Uppercase "R" is different from lowercase "r"
console.log(red); // Output: -1
const arr = [1, 2, 3, 4, 5];
const element = 3;

if (arr.indexOf(element) !== -1) {
    console.log(`${element} exists in the array.`);
} else {
    console.log(`${element} does not exist in the array.`);
}

// Output: "3 exists in the array."
const arr = [1, 2, 2, 3, 4, 4, 5];
const uniqueArr = [];

arr.forEach(element => {
    if (uniqueArr.indexOf(element) === -1) {
        uniqueArr.push(element);
    }
});

console.log(uniqueArr); // Output: [1, 2, 3, 4, 5]
const arr = [1, 2, 2, 3, 4, 4, 5];
const uniqueArr = Array.from(new Set(arr));

console.log(uniqueArr); // Output: [1, 2, 3, 4, 5]
const arr = [1, 2, 3, 4, 5];
const element = 3;
const startIndex = 1;
const endIndex = 3;
const subArray = arr.slice(startIndex, endIndex + 1);

if (subArray.indexOf(element) !== -1) {
    console.log(`${element} exists within the specified range of the array.`);
} else {
    console.log(`${element} does not exist within the specified range of the array.`);
}

// Output: "3 exists within the specified range of the array."