// Array to filter
const num = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

// Callback function defined by the developer
function isEven(value) {
    return value % 2 === 0; // Returns true if the value is even
}

// Filter the array elements using the callback function
const result = num.filter(isEven);
console.log(result); // Output: [2, 4, 6, 8, 10]
/* The filter() function is used to
   select only the elements that meet a certain condition from an array. */

// For example, filtering only lowercase letters from an array
['a', 'B', 'c', 'D'].filter(isLowerCase) 👉 ['a', 'c']
arr.filter(callbackFn[, thisArg])
// Arrow function
filter((element) => { /* … */ })
filter((element[, index]) => { /* … */ })
filter((element[, index[, array]]) => { /* … */ })

// Callback function reference
filter(callbackFn)
filter(callbackFn[, thisArg])

// Inline callback function
filter(function (element) { /* … */ })
filter(function (element[, index]) { /* … */ })
filter(function (element[, index[, array]]) { /* … */ })
filter(function (element[, index[, array]]) { /* … */ }[, thisArg])
callbackFn(element[, index[, array]])
/**
 * Callback function
 *
 * @param {*} element The current array element
 * @param {number} index Optional. The index of the current element
 * @param {Array} array Optional. The original array
 * @return {boolean} Return true if the element meets the filtering condition, false otherwise
 *
 * The callback can be a named function (user-defined) or an anonymous function.
 * It can also be written as an arrow function.
 */

// Using a named function
function callbackFn(element[, index[, array]]) {
    // Filtering logic: return a boolean to determine if the element should be included
}

arr.filter(callbackFn); // Pass the named function as the parameter

// Using an anonymous function
arr.filter(function (element[, index[, array]]) {
    // Filtering logic: return a boolean to determine if the element should be included
});
const numbers = [1, 2, 2, 3, 4, 4, 5];

const uniqueNumbers = numbers.filter((element, index, arr) => {
    // Return true only if the element has not appeared before
    return arr.indexOf(element) === index;
});

console.log(uniqueNumbers); // Output: [1, 2, 3, 4, 5]
const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9];

const filteredNumbers = numbers.filter(element => {
    // Combine multiple conditions
    return element % 2 === 0 && element < 5;
});

console.log(filteredNumbers); // Output: [2, 4]
const students = [
    ["Alice", 25],
    ["Bob", 30],
    ["Charlie", 22],
    ["David", 35]
];

const filteredStudents = students.filter(student => {
    // Return true only if the second element (age) is 30 or older
    return student[1] >= 30;
});

console.log(filteredStudents); // Output: [["Bob", 30], ["David", 35]]
const words = ["apple", "banana", "cherry", "date", "fig"];
const longWords = words.filter(word => word.length >= 5);
console.log(longWords); // Output: ["apple", "banana", "cherry"]