Last active
April 4, 2023 20:56
-
-
Save kddial/c83525f6bfb9bf85b1c48fa8f4398ab6 to your computer and use it in GitHub Desktop.
interview
This file contains 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
// OLD FUNCTION | |
const findSKUFromVariant = (productItem, variant) => { | |
return productItem.skus.find(sku => sku.variant.color === variant.color) | |
}; | |
// NEW FUNCTION | |
const findSKUFromVariant = (productItem, variant) => { | |
// compare sku item based on new varient and previously set varients | |
function compareVarient(item, selectedItem) { | |
const allVarients = Object.keys(selectedItem); | |
for (let i = 0; i < allVarients.length; i++) { | |
const category = allVarients[i]; | |
if (item[category] !== selectedItem[category]) { | |
return false; | |
} | |
} | |
return true; // all categories match (both color and size) | |
} | |
return productItem.skus.find((sku) => { | |
return compareVarient(sku.variant, variant); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment