Last active
February 8, 2023 14:58
-
-
Save elinardo10/81e5fb3a63c578b68d73f66ff33ecc8a to your computer and use it in GitHub Desktop.
Desafio do Mestre Tiago Matos
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
// 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