Duplicate Array in Function

Asked

Viewed 40 times

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)

The result that if expected would be this: inserir a descrição da imagem aqui

1 answer

0


Is duplicating the result because you are invoking twice the method Cal_Gorjeta():

The first call is made inline:

let gorjeta = info_comprador.Cal_Gorjeta()

The second is done within the method Cal_Total():

Cal_Total: function() {
                            //Essa chamada que está duplicando o array
                            let gorjeta = this.Cal_Gorjeta()

                            let n = 0
                            while (n < this.gasto_rest.length) {
                                //Aqui você está usando variével local gorjeta
                                //quando já possui a propriedade gorjetas
                                this.Total_Gasto += this.gasto_rest[n] + gorjeta[n]

                                n ++
                            } return this.Total_Gasto
            }

To fix you can just comment on the line let gorjeta = this.Cal_Gorjeta() within the method Cal_Total or create a call condition on that line. Use the property gorjetas to calculate Total_Gasto.

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() {
    //No caso eu deixei a linha comentada pois desconheço o resto do seu código
    //mas caso precise de uma condição de uso deixo como referência.
    //if (this.gorjetas == null || this.gorjetas.length === 0) this.Cal_Gorjeta()
    let n = 0
    while (n < this.gasto_rest.length) {
      this.Total_Gasto += this.gasto_rest[n] + this.gorjetas[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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.