Skip to content

Instantly share code, notes, and snippets.

@elinardo10
Last active February 8, 2023 14:58
Show Gist options
  • Save elinardo10/81e5fb3a63c578b68d73f66ff33ecc8a to your computer and use it in GitHub Desktop.
Save elinardo10/81e5fb3a63c578b68d73f66ff33ecc8a to your computer and use it in GitHub Desktop.
Desafio do Mestre Tiago Matos
// Faça o calculo do total destes produtos que estão no carrinho
const cart = [
{
name: 'Pizza de calebreza',
amount: 10,
qty: 2,
addons: [
{
'name': 'Extra calabreza',
'amount': 1.5
},
{
'name': 'Borda recheada',
'amount': 7
},
],
},
{
name: 'Cane assada',
amount: 35,
qty: 1,
}
];
function calcAddions(addons){
return addons.reduce((itemTotal, item) => {
itemTotal += item.amount;
return itemTotal;
}, 0);
}
cart.reduce((total, product) => {
let addionsTotal = 0;
if(product.addons){
addionsTotal = calcAddions(product.addons)
}
total += (product.amount + addionsTotal) * (product.qty ?? 1);
return total;
}, 0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment