Created
February 7, 2023 21:56
-
-
Save brasizza/ee40e8428826807fa6f2e063c5fe10a5 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
const cart = [{ | |
name: 'Pizza de calebreza', | |
amount: 10, | |
qty: 2, | |
addons: [{ | |
'name': 'Extra calabreza', | |
'amount': 1.5 | |
}, | |
{ | |
'name': 'Borda recheada', | |
'amount': 7 | |
}, | |
], | |
}, | |
{ | |
name: 'Carne assada', | |
amount: 35, | |
qty: 1, | |
} | |
]; | |
let totalConta = 0; | |
for (const item of cart) { | |
const quantidade = item.qty; | |
totalConta += item.amount * quantidade; | |
if (item.addons) { | |
for (const addon of item.addons) { | |
totalConta += addon.amount * quantidade; | |
} | |
} | |
} | |
console.log(Intl.NumberFormat('pt-BR', { | |
style: 'currency', | |
currency: 'BRL' | |
}) | |
.format(totalConta)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment