Created
November 7, 2023 03:53
-
-
Save AadityaJain-Dev/b4104de46174d483300532159525629a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Filter products on the Myntra eCommerce website based on a specified price range. | |
* This code selects product elements and hides/shows them based on their prices. | |
* | |
* @param {number} minPrice - Minimum price threshold. | |
* @param {number} maxPrice - Maximum price threshold. | |
*/ | |
function filterProductsByPriceRange(minPrice, maxPrice) { | |
// Select all product items on the Myntra search results page. | |
const productItems = document.querySelectorAll("#desktopSearchResults > div.search-searchProductsContainer.row-base > section > ul > li"); | |
productItems.forEach((productItem) => { | |
// Attempt to find the discounted price element, if available. | |
let productPriceElement = productItem.querySelector("a > div.product-productMetaInfo > div > span:nth-child(1) > span.product-discountedPrice"); | |
if (!productPriceElement) { | |
// If discounted price element is not found, use MRP as the price. | |
productPriceElement = productItem.querySelector("a > div.product-productMetaInfo > div > span"); | |
} | |
if (!productPriceElement) { | |
// If price element is still not found, skip this product. | |
return; | |
} | |
// Extract the product price from the text and convert it to an integer. | |
const productPriceText = productPriceElement.innerText.replace("Rs. ", ""); | |
const productPrice = parseInt(productPriceText, 10); | |
if (productPrice >= minPrice && productPrice <= maxPrice) { | |
// Product price is within the specified range, keep it visible. | |
productItem.style.display = "block"; | |
} else { | |
// Product price is outside the specified range, hide it. | |
productItem.style.display = "none"; | |
} | |
}); | |
} | |
// Define the price range to filter products. | |
const minPrice = 0; | |
const maxPrice = 2000; | |
// Call the function to filter products by price range. | |
filterProductsByPriceRange(minPrice, maxPrice); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment