// ---------------------------------------------
// Basket: a mixed list of fruits and vegetables
// Each item has properties describing what it is
// ---------------------------------------------
const basket = [
{ icon: "🍎", hasSeeds: true, color: "red", weight: 180, sweetness: 6, price: 1.2, category: "fruit" },
{ icon: "🥕", hasSeeds: false, color: "orange", weight: 80, sweetness: 4, price: 0.7, category: "vegetable" },
{ icon: "🍌", hasSeeds: false, color: "yellow", weight: 120, sweetness: 8, price: 1.0, category: "fruit" },
{ icon: "🥦", hasSeeds: false, color: "green", weight: 300, sweetness: 3, price: 1.8, category: "vegetable" },
{ icon: "🍓", hasSeeds: true, color: "red", weight: 25, sweetness: 9, price: 0.5, category: "fruit" },
{ icon: "🍆", hasSeeds: true, color: "purple", weight: 250, sweetness: 2, price: 2.1, category: "vegetable" },
{ icon: "🥝", hasSeeds: true, color: "green", weight: 75, sweetness: 7, price: 1.3, category: "fruit" },
{ icon: "🍍", hasSeeds: false, color: "yellow", weight: 900, sweetness: 5, price: 3.5, category: "fruit" }
];
/**
* Custom implementation of Array.prototype.some()
* ------------------------------------------------
* @param {Array} array - the list we want to check
* @param {Function} fxn - the test function applied to each element
* @returns {boolean} - true if at least one element passes the test
*
* How it works:
* - Loop through the array
* - Apply the callback to each element
* - If the callback returns true → stop and return true
* - If no element matches → return false
*/
function mySome(array, fxn) {
for (let i = 0; i < array.length; i++) {
if (fxn(array[i])) {
return true; // Found at least one matching item
}
}
return false; // No match found
}
// ------------------------------------------------------
// Example 1: Check if the basket contains a vegetable
// Using a classic function expression
// ------------------------------------------------------
const hasVegetable = mySome(
basket,
function (basketItem) {
return basketItem.category === "vegetable";
}
);
console.log("Has vegetable?", hasVegetable); // true
// ------------------------------------------------------
// Example 2: Same test, but using an arrow function
// ------------------------------------------------------
const hasVegetable2 = mySome(
basket,
basketItem => basketItem.category === "vegetable"
);
console.log("Has vegetable?", hasVegetable2); // true
// ------------------------------------------------------
// Example 3: Store the callback in a variable
// ------------------------------------------------------
const hasVegetable3 = basketItem => basketItem.category === "vegetable";
const hasVegetable3Result = mySome(basket, hasVegetable3);
console.log("Has vegetable?", hasVegetable3Result); // true
// ------------------------------------------------------
// Example 4: Check if the basket contains an expensive item
// ------------------------------------------------------
const hasExpensiveItem = mySome(
basket,
basketItem => basketItem.price > 3
);
console.log("Has expensive item?", hasExpensiveItem); // true (🍍)
// ------------------------------------------------------
// Example 5: Check if the basket contains a seedless fruit
// ------------------------------------------------------
const seedlessFruit = mySome(
basket,
basketItem => basketItem.category === "fruit" && !basketItem.hasSeeds
);
console.log("Has seedless fruit?", seedlessFruit); // true (🍌, 🍍)