My goal is to create a function to find the most cost-effective combination of products for a given budget.
Expected Output: cost-effective combination of two products:
["Shirt", "Hat"] // (costing 20 + 15 = 35)
I am trying to solve this javascript problem but I am not able to add the prices in correct pair and then compare them. If anyone could help and explain it I’ll be thank full
let shoppingListCost = [{
name: "Shirt",
price: 20,
quantity: 5
}, {
name: "Jeans",
price: 40,
quantity: 2
}, {
name: "Hat",
price: 15,
quantity: 8
}]
function costEffectiveCombinations(array) {
let pairList = []
let priced = 0;
let lessprice = priced;
for (let i = 0; i < array.length; i++) {
console.log(array[i].price)
for (let j = i++; j < array.length; j++) {
priced = array[i].price + array[j].price
if (lessprice < priced) {
lessprice = priced
}
}
}
console.log(lessprice);
return pairList
};
console.log(costEffectiveCombinations(shoppingListCost));
I got stuck and received error and don’t know how to proceed further!
New contributor
AAR EMPIRE is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
4