0
I created a script where inside my object it would calculate the percentage of an array, in this same object it contains a function that calculates the sum of my percentage with my array. However when I call the function of the object responsible for the percentage in the output it duplicates my array.
const info_comprador = {
Nome_Comprador: 'João',
gasto_rest: [50,100,300,10,80],
gorjetas: [],
Total_Gasto: 0,
Cal_Gorjeta: function() {
let n = 0
while (n < this.gasto_rest.length) {
if (this.gasto_rest[n] < 50) {
this.gorjetas.unshift(this.gasto_rest[n] * 0.2);
} else if (this.gasto_rest[n] > 50 && this.gasto_rest[n] < 200) {
this.gorjetas.unshift(this.gasto_rest[n] * .15);
} else {
this.gorjetas.unshift(this.gasto_rest[n] * .1);
} n++
} return this.gorjetas
},
Cal_Total: function() {
let gorjeta = this.Cal_Gorjeta()
let n = 0
while (n < this.gasto_rest.length) {
this.Total_Gasto += this.gasto_rest[n] + gorjeta[n]
n ++
} return this.Total_Gasto
}
}
let gorjeta = info_comprador.Cal_Gorjeta()
let total = info_comprador.Cal_Total()
console.log(gorjeta,total)
Yes, that’s exactly it, my mistake was thinking that I would have to call again, but when the Calc_tip function is called outside the object my tip array is already added was just calling the array
– user77295