Created
June 25, 2024 08:10
-
-
Save andrewjamesford/e126f83f526de237d6bc43e519e05674 to your computer and use it in GitHub Desktop.
Some code that needs to be refactored
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
function processProducts(products) { | |
let totalPrice = 0; | |
for (let i = 0; i < products.length; i++) { | |
const product = products[i]; | |
if (product.price > 100) { | |
product.discountedPrice = product.price * 0.8; // 20% discount for products over $100 | |
} else if (product.price > 50) { | |
product.discountedPrice = product.price * 0.9; // 10% discount for products over $50 | |
} else { | |
product.discountedPrice = product.price; | |
} | |
if (product.stock === 0) { | |
product.isAvailable = false; | |
} else { | |
product.isAvailable = true; | |
} | |
totalPrice += product.discountedPrice; | |
} | |
console.log(totalPrice); | |
return products; | |
} | |
const products = [ | |
{ name: "Product 1", price: 120, stock: 10 }, | |
{ name: "Product 2", price: 60, stock: 0 }, | |
{ name: "Product 3", price: 30, stock: 5 }, | |
]; | |
processProducts(products); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment