Skip to content

Instantly share code, notes, and snippets.

@leo07vasp

leo07vasp/desconto.js

Last active Oct 11, 2019
Embed
What would you like to do?
desconto.js
const items = [
{
nome:'exame 1',
valor: 10
},
{
nome:'exame 2',
valor: 12
},
{
nome:'exame 3',
valor: 13
},
{
nome:'exmae 4',
valor: 19
}];
itensDesc = [];
let valorTotal = items.reduce(function(a, b) { return a + b.valor; }, 0);
let Desconto = 10;
for( key in items){
let descontoProp = items[key].valor / valorTotal;
let descontoAplica = descontoProp * Desconto;
//console.log((items[key].valor - descontoAplica).toFixed(2));
itensDesc.push(items[key]);
}
let ItensComDesconto = items.reduce(function(newArr, preV, i){
let descontoProp = preV.valor / valorTotal;
let descontoAplica = descontoProp * Desconto;
preV = (preV.valor - descontoAplica);
//console.log(newArr);
//console.log(preV.valor - descontoAplica);
//console.log(preV.valor - descontoAplica);
newArr[i] = preV;
return newArr;
}, []);
console.log(valorTotal);
console.log(Desconto);
console.log(valorTotal - Desconto);
console.log('---------------------------------');
console.log(items);
console.log('---------------------------------');
console.log(ItensComDesconto);
console.log(ItensComDesconto.reduce(function(a, b) { return a + b; }, 0).toFixed(2));
console.log('---------------------------------');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment