Last active
May 11, 2023 17:20
-
-
Save muslemomar/90221cad0981fbde8784c9f967998b96 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
/** | |
* Calculates the discounted price of a product based on the original price and discount percentage. | |
* | |
* @param {number} originalPrice - The original price of the product. | |
* @param {number} discountPercentage - The discount percentage to be applied. | |
* @returns {number} The calculated discounted price. | |
*/ | |
function calculateDiscountedPrice(originalPrice, discountPercentage) { | |
const discountAmount = (originalPrice * discountPercentage) / 100; | |
const discountedPrice = originalPrice - discountAmount; | |
return discountedPrice; | |
} | |
/** | |
* Sorts an array of objects based on the value of a property. | |
* @param {Array} arr - The array of objects to sort. | |
* @param {string} prop - The name of the property to sort by. | |
* @param {string} order - The sort order: 'asc' for ascending or 'desc' for descending. Default is 'asc'. | |
* @returns {Array} The sorted array. | |
*/ | |
function sortByProperty(arr, prop, order = 'asc') { | |
const sortedArr = arr.sort((a, b) => { | |
const propA = a[prop]; | |
const propB = b[prop]; | |
if (propA < propB) { | |
return order === 'asc' ? -1 : 1; | |
} else if (propA > propB) { | |
return order === 'asc' ? 1 : -1; | |
} else { | |
return 0; | |
} | |
}); | |
return sortedArr; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment