Last active
February 7, 2023 01:43
-
-
Save russo97/6056e66852cdb0da743db7812c7447b3 to your computer and use it in GitHub Desktop.
Resolução do desafio #1 do Tiago Matos, liberado no Instagram
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: 'Carne assada', | |
amount: 35, | |
qty: 1, | |
} | |
]; | |
function deepSum (list) { | |
return list.reduce((acc, cur) => { | |
const quantity = cur.qty ?? 1; | |
const sum = acc + cur.amount * quantity; | |
return cur.addons | |
? sum + quantity * deepSum(cur.addons) | |
: sum; | |
}, 0); | |
} | |
console.log(deepSum(cart)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment