Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
</body>
</html>
 
// Products base data
var productsData = {
    a: {
        id: 2759167427,
        title: 'Practical Silk Bag',
        handle: 'practical-silk-bag',
        vendor: 'Legros, Willms and Von',
        product_type: 'Bag',
    },
    b: {
        id: 2759167417,
        title: 'Practical Silk Bag 2',
        handle: 'practical-silk-bag-2',
        vendor: 'Legros, Willms and Von 2',
        product_type: 'Bag 2',
    },
    c: {
        id: 2759167417,
        title: 'Practical Silk Bag 3',
        handle: 'practical-silk-bag-3',
        vendor: 'Legros, Willms and Von 3',
        product_type: 'Computer', // This product must be returned
    },
    d: {
        id: 2759167417,
        title: 'Practical Silk Bag 4',
        handle: 'practical-silk-bag-4',
        vendor: 'Legros, Willms and Von 4',
        product_type: 'Keyboard', // This product must be returned
    }
};
/**
 * Function to find products by any condition
 */
function searchItemsByCondition(products, evaluateCondition) {
    var ans = [];
    for (var item in productsData) {
        // Making sure the object has the property product_type
        if (products[item].hasOwnProperty('product_type')) {
            if (evaluateCondition(products[item])) {
                ans.push(products[item]);
            }
        }
    }
    return ans;
}
function searchByKeyboardOrComputer(product) {
    return (product.product_type === 'Computer') || (product.product_type === 'Keyboard');
}
// Call the function passing the evaluation function to satisfy.
// It should log only the items with 'Keyboard' or 'Computer' product_type
console.log(searchItemsByCondition(productsData, searchByKeyboardOrComputer));
Output 300px

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
wilsotobiancopro
0viewers